home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / gdb / mipsread.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  72.5 KB  |  2,776 lines

  1. /* Read a symbol table in MIPS' format (Third-Eye).
  2.    Copyright (C) 1986, 1987, 1989-1991 Free Software Foundation, Inc.
  3.    Contributed by Alessandro Forin (af@cs.cmu.edu) at CMU
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* This module provides three functions: mipscoff_symfile_init,
  22.    which initializes to read a symbol file; mipscoff_new_init, which 
  23.    discards existing cached information when all symbols are being
  24.    discarded; and mipscoff_symfile_read, which reads a symbol table
  25.    from a file.
  26.  
  27.    mipscoff_symfile_read only does the minimum work necessary for letting the
  28.    user "name" things symbolically; it does not read the entire symtab.
  29.    Instead, it reads the external and static symbols and puts them in partial
  30.    symbol tables.  When more extensive information is requested of a
  31.    file, the corresponding partial symbol table is mutated into a full
  32.    fledged symbol table by going back and reading the symbols
  33.    for real.  mipscoff_psymtab_to_symtab() is called indirectly through
  34.    a pointer in the psymtab to do this.  */
  35.  
  36. #include <stdio.h>
  37. #include "param.h"
  38. #include "obstack.h"
  39. #include <sys/param.h>
  40. #include <sys/file.h>
  41. #include <sys/stat.h>
  42. #include "defs.h"
  43. #include "symtab.h"
  44. #include "gdbcore.h"
  45. #include "symfile.h"
  46. #ifdef    CMUCS
  47. #include <mips/syms.h>
  48. #else /* not CMUCS */
  49. #include <symconst.h>
  50. #include <sym.h>
  51. #endif /* not CMUCS */
  52.  
  53. #include "ecoff.h"
  54.  
  55. struct coff_exec {
  56.     struct external_filehdr f;
  57.     struct external_aouthdr a;
  58. };
  59.  
  60. /* Things we import explicitly from other modules */
  61.  
  62. extern int         info_verbose;
  63. extern struct block *block_for_pc();
  64. extern void         sort_symtab_syms();
  65.  
  66. /* Various complaints about symbol reading that don't abort the process */
  67.    
  68. struct complaint unknown_ext_complaint = 
  69.     {"unknown external symbol %s", 0, 0};
  70.  
  71. struct complaint unknown_sym_complaint = 
  72.     {"unknown local symbol %s", 0, 0};
  73.  
  74. struct complaint unknown_st_complaint = 
  75.     {"with type %d", 0, 0};
  76.  
  77. struct complaint block_overflow_complaint = 
  78.     {"block containing %s overfilled", 0, 0};
  79.  
  80. struct complaint basic_type_complaint = 
  81.     {"cannot map MIPS basic type 0x%x", 0, 0};
  82.  
  83. struct complaint unknown_type_qual_complaint = 
  84.     {"unknown type qualifier 0x%x", 0, 0};
  85.  
  86. struct complaint array_bitsize_complaint = 
  87.     {"size of array target type not known, assuming %d bits", 0, 0};
  88.  
  89. struct complaint array_parse_complaint = 
  90.     {"array type with strange relative symbol", 0, 0};
  91.  
  92. /* Macros and extra defs */
  93.  
  94. /* Already-parsed symbols are marked specially */
  95.  
  96. #define stParsed stType
  97.  
  98. /* Puns: hard to find whether -g was used and how */
  99.  
  100. #define MIN_GLEVEL GLEVEL_0
  101. #define compare_glevel(a,b)                    \
  102.     (((a) == GLEVEL_3) ? ((b) < GLEVEL_3) :            \
  103.      ((b) == GLEVEL_3) ? -1 : (int)((b) - (a)))
  104.  
  105. /* When looking at .o files, avoid tripping over bad addresses */
  106.  
  107. #define SAFE_TEXT_ADDR 0x400000
  108. #define SAFE_DATA_ADDR 0x10000000
  109.  
  110. #define UNSAFE_DATA_ADDR(p)    ((unsigned)p < SAFE_DATA_ADDR || (unsigned)p > 2*SAFE_DATA_ADDR)
  111.  
  112. /* Things that really are local to this module */
  113.  
  114. /* GDB symtable for the current compilation unit */
  115.  
  116. static struct symtab *cur_stab;
  117.  
  118. /* MIPS symtab header for the current file */
  119.  
  120. static HDRR    *cur_hdr;
  121.  
  122. /* Pointer to current file decriptor record, and its index */
  123.  
  124. static FDR    *cur_fdr;
  125. static int     cur_fd;
  126.  
  127. /* Index of current symbol */
  128.  
  129. static int     cur_sdx;
  130.  
  131. /* Note how much "debuggable" this image is.  We would like
  132.    to see at least one FDR with full symbols */
  133.  
  134. static max_gdbinfo;
  135. static max_glevel;
  136.  
  137. /* When examining .o files, report on undefined symbols */
  138.  
  139. static int n_undef_symbols, n_undef_labels, n_undef_vars, n_undef_procs;
  140.  
  141. /* Extra builtin types */
  142.  
  143. struct type *builtin_type_complex;
  144. struct type *builtin_type_double_complex;
  145. struct type *builtin_type_fixed_dec;
  146. struct type *builtin_type_float_dec;
  147. struct type *builtin_type_string;
  148.  
  149. /* Template types */
  150.  
  151. static struct type *builtin_type_ptr;
  152. static struct type *builtin_type_struct;
  153. static struct type *builtin_type_union;
  154. static struct type *builtin_type_enum;
  155. static struct type *builtin_type_range;
  156. static struct type *builtin_type_set;
  157.  
  158. /* Forward declarations */
  159.  
  160. static struct symbol    *new_symbol();
  161. static struct type    *new_type();
  162. static struct field    *new_field();
  163. static struct block    *new_block();
  164. static struct symtab    *new_symtab();
  165. static struct linetable    *new_linetable();
  166. static struct blockvector *new_bvect();
  167.  
  168. static struct type    *parse_type();
  169. static struct type    *make_type();
  170. static struct symbol    *mylookup_symbol();
  171. static struct block    *shrink_block();
  172.  
  173. static int compare_symtabs();
  174. static int compare_psymtabs();
  175. static int compare_blocks();
  176.  
  177. static struct partial_symtab *new_psymtab();
  178. static struct partial_symtab *parse_fdr();
  179. static int compare_psymbols();
  180.  
  181. static void psymtab_to_symtab_1();
  182. static void add_block();
  183. static void add_symbol();
  184. static int  add_line();
  185. static void reorder_symtabs();
  186. static void reorder_psymtabs();
  187. static void shrink_linetable();
  188.  
  189. /* Things we export to other modules */
  190.  
  191. /* Address bounds for the signal trampoline in inferior, if any */
  192. /* FIXME:  Nothing really seems to use this.  Why is it here? */
  193.  
  194. CORE_ADDR sigtramp_address, sigtramp_end;
  195.  
  196. /* The entry point (starting address) of the file, if it is an executable.  */
  197.  
  198. static CORE_ADDR entry_point;
  199.  
  200. extern CORE_ADDR startup_file_start;    /* From blockframe.c */
  201. extern CORE_ADDR startup_file_end;    /* From blockframe.c */
  202.  
  203. void
  204. mipscoff_new_init()
  205. {
  206.   /* If we have a file symbol header lying around, blow it away.  */
  207.   if (cur_hdr)
  208.     free ((char *)cur_hdr);
  209.   cur_hdr = 0;
  210. }
  211.  
  212. void
  213. mipscoff_symfile_init (sf)
  214.      struct sym_fns *sf;
  215. {
  216.   bfd *abfd = sf->sym_bfd;
  217.   sf->sym_private = NULL;
  218.  
  219.   /* Save startup file's range of PC addresses to help blockframe.c
  220.      decide where the bottom of the stack is.  */
  221.   if (bfd_get_file_flags (abfd) & EXEC_P)
  222.     {
  223.       /* Executable file -- record its entry point so we'll recognize
  224.      the startup file because it contains the entry point.  */
  225.       entry_point = bfd_get_start_address (abfd);
  226.     }
  227.   else
  228.     {
  229.       /* Examination of non-executable.o files.  Short-circuit this stuff.  */
  230.       /* ~0 will not be in any file, we hope.  */
  231.       entry_point = ~0;
  232.       /* set the startup file to be an empty range.  */
  233.       startup_file_start = 0;
  234.       startup_file_end = 0;
  235.     }
  236. }
  237.  
  238. void
  239. mipscoff_symfile_read(sf, addr, mainline)
  240.      struct sym_fns *sf;
  241.      CORE_ADDR addr;
  242.      int mainline;
  243. {
  244.   struct coff_symfile_info *info = (struct coff_symfile_info *)sf->sym_private;
  245.   bfd *abfd = sf->sym_bfd;
  246.   char *name = bfd_get_filename (abfd);
  247.   int desc;
  248.   register int val;
  249.   int symtab_offset;
  250.   int stringtab_offset;
  251.  
  252. /* WARNING WILL ROBINSON!  ACCESSING BFD-PRIVATE DATA HERE!  FIXME!  */
  253.    desc = fileno ((FILE *)(abfd->iostream));    /* Raw file descriptor */
  254. /* End of warning */
  255.  
  256.   /* Position to read the symbol table.  */
  257.   val = lseek (desc, (long)symtab_offset, 0);
  258.   if (val < 0)
  259.     perror_with_name (name);
  260.  
  261.   init_misc_bunches ();
  262.   make_cleanup (discard_misc_bunches, 0);
  263.  
  264.   /* Now that the executable file is positioned at symbol table,
  265.      process it and define symbols accordingly.  */
  266.  
  267.   read_mips_symtab(abfd, desc);
  268.  
  269.   /* Go over the misc symbol bunches and install them in vector.  */
  270.  
  271.   condense_misc_bunches (!mainline);
  272. }
  273.   
  274. /* Exported procedure: Allocate zeroed memory */
  275.  
  276. char *xzalloc(size)
  277. {
  278.     char           *p = xmalloc(size);
  279.  
  280.     bzero(p, size);
  281.     return p;
  282. }
  283.  
  284. /* Exported procedure: Builds a symtab from the PST partial one.
  285.    Restores the environment in effect when PST was created, delegates
  286.    most of the work to an ancillary procedure, and sorts
  287.    and reorders the symtab list at the end */
  288.  
  289. static void
  290. mipscoff_psymtab_to_symtab(pst)
  291.     struct partial_symtab *pst;
  292. {
  293.     struct symtab  *ret;
  294.     int             i;
  295.  
  296.     if (!pst)
  297.         return;
  298.  
  299.     if (info_verbose) {
  300.         printf_filtered("Reading in symbols for %s...", pst->filename);
  301.         fflush(stdout);
  302.     }
  303.     /* Restore the header and list of pending typedefs */
  304.     /* FIXME, we should use private data that is a proper pointer. */
  305.     cur_hdr = (HDRR *) pst->ldsymlen;
  306.  
  307.     psymtab_to_symtab_1(pst, pst->filename);
  308.  
  309.     reorder_symtabs();
  310.  
  311.     if (info_verbose)
  312.         printf_filtered("done.\n");
  313. }
  314.  
  315. /* Exported procedure: Is PC in the signal trampoline code */
  316.  
  317. int
  318. in_sigtramp(pc, name)
  319.     CORE_ADDR pc;
  320.     char *name;
  321. {
  322.     if (sigtramp_address == 0)
  323.         fixup_sigtramp();
  324.     return (pc >= sigtramp_address && pc < sigtramp_end);
  325. }
  326.  
  327. /* File-level interface functions */
  328.  
  329. /* Read the symtab information from file FSYM into memory.  Also,
  330.    return address just past end of our text segment in *END_OF_TEXT_SEGP.  */
  331.  
  332. static
  333. read_the_mips_symtab(abfd, fsym, end_of_text_segp)
  334.     bfd         *abfd;
  335.     int        fsym;
  336.     CORE_ADDR    *end_of_text_segp;
  337. {
  338.     int             stsize, st_hdrsize;
  339.     unsigned        st_filptr;
  340.     HDRR            st_hdr;
  341.     /* Header for executable/object file we read symbols from */
  342.     struct coff_exec filhdr;
  343.  
  344.     /* We get here with DESC pointing to the symtab header. But we need
  345.      * other info from the initial headers */
  346.     lseek(fsym, 0L, 0);
  347.     myread(fsym, &filhdr, sizeof filhdr);
  348.  
  349.     if (end_of_text_segp)
  350.         *end_of_text_segp =
  351.             bfd_h_get_32 (abfd, filhdr.a.text_start) +
  352.             bfd_h_get_32 (abfd, filhdr.a.tsize);
  353.  
  354.     /* Find and read the symbol table header */
  355.     st_hdrsize = bfd_h_get_32 (abfd, filhdr.f.f_nsyms);
  356.     st_filptr  = bfd_h_get_32 (abfd, filhdr.f.f_symptr);
  357.     if (st_filptr == 0)
  358.         return 0;
  359.  
  360.     lseek(fsym, st_filptr, L_SET);
  361.     if (st_hdrsize > sizeof (st_hdr))    /* Profanity check */
  362.         abort();
  363.     if (read(fsym, &st_hdr, st_hdrsize) != st_hdrsize)
  364.         goto readerr;
  365.  
  366.     /* Find out how large the symbol table is */
  367.     stsize = (st_hdr.cbExtOffset - (st_filptr + st_hdrsize))
  368.         + st_hdr.iextMax * cbEXTR;
  369.  
  370.     /* Allocate space for the symbol table.  Read it in.  */
  371.     cur_hdr = (HDRR *) xmalloc(stsize + st_hdrsize);
  372.  
  373.     bcopy(&st_hdr, cur_hdr, st_hdrsize);
  374.     if (read(fsym, (char *) cur_hdr + st_hdrsize, stsize) != stsize)
  375.         goto readerr;
  376.  
  377.     /* Fixup file_pointers in it */
  378.     fixup_symtab(cur_hdr, (char *) cur_hdr + st_hdrsize,
  379.              st_filptr + st_hdrsize);
  380.  
  381.     return;
  382. readerr:
  383.     error("Short read on %s", symfile);
  384. }
  385.  
  386.  
  387. /* Turn all file-relative pointers in the symtab described by HDR
  388.    into memory pointers, given that the symtab itself is located
  389.    at DATA in memory and F_PTR in the file. */
  390.  
  391. static
  392. fixup_symtab( hdr, data, f_ptr)
  393.     HDRR *hdr;
  394.     char *data;
  395. {
  396.     int             f_idx, s_idx;
  397.     FDR            *fh;
  398.     SYMR           *sh;
  399.     OPTR           *op;
  400.     PDR           *pr;
  401.     EXTR           *esh;
  402.  
  403.     /*
  404.      * These fields are useless (and empty) by now:
  405.      *    hdr->cbDnOffset, hdr->cbOptOffset
  406.      * We use them for other internal purposes.
  407.      */
  408.     hdr->cbDnOffset = 0;
  409.     hdr->cbOptOffset = 0;
  410.  
  411. #define FIX(off) \
  412.         if (hdr->off) hdr->off = (unsigned int)data + (hdr->off - f_ptr);
  413.  
  414.         FIX(cbLineOffset);
  415.         FIX(cbPdOffset);
  416.         FIX(cbSymOffset);
  417.         FIX(cbOptOffset);
  418.         FIX(cbAuxOffset);
  419.         FIX(cbSsOffset);
  420.         FIX(cbSsExtOffset);
  421.         FIX(cbFdOffset);
  422.         FIX(cbRfdOffset);
  423.         FIX(cbExtOffset);
  424. #undef    FIX
  425.  
  426.  
  427.     /*
  428.      * Fix all string pointers inside the symtab, and
  429.      * the FDR records.  Also fix other miscellany.
  430.      */
  431.     for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) {
  432.         register unsigned code_offset;
  433.  
  434.         /* Header itself, and strings */
  435.         fh = (FDR *) (hdr->cbFdOffset) + f_idx;
  436.         fh->issBase += hdr->cbSsOffset;
  437.         if (fh->rss != -1)
  438.             fh->rss = (long)fh->rss + fh->issBase;
  439.         for (s_idx = 0; s_idx < fh->csym; s_idx++) {
  440.             sh = (SYMR*)(hdr->cbSymOffset) + fh->isymBase + s_idx;
  441.             sh->iss = (long) sh->iss + fh->issBase;
  442.             sh->reserved = 0;
  443.         }
  444.  
  445.         cur_fd = f_idx;
  446.  
  447.         /* Local symbols */
  448.         fh->isymBase = (int)((SYMR*)(hdr->cbSymOffset)+fh->isymBase);
  449.  
  450.         /* cannot fix fh->ipdFirst because it is a short */
  451. #define IPDFIRST(h,fh) \
  452.         ((long)h->cbPdOffset + fh->ipdFirst * sizeof(PDR))
  453.  
  454.         /* Optional symbols (actually used for partial_symtabs) */
  455.         fh->ioptBase = 0;
  456.         fh->copt = 0;
  457.  
  458.         /* Aux symbols */
  459.         if (fh->caux)
  460.             fh->iauxBase = hdr->cbAuxOffset + fh->iauxBase * sizeof(AUXU);
  461.         /* Relative file descriptor table */
  462.         fh->rfdBase = hdr->cbRfdOffset + fh->rfdBase * sizeof(RFDT);
  463.  
  464.         /* Line numbers */
  465.         if (fh->cbLine)
  466.             fh->cbLineOffset += hdr->cbLineOffset;
  467.  
  468.         /* Procedure symbols.  (XXX This should be done later) */
  469.         code_offset = fh->adr;
  470.         for (s_idx = 0; s_idx < fh->cpd; s_idx++) {
  471.             unsigned name, only_ext;
  472.  
  473.             pr = (PDR*)(IPDFIRST(hdr,fh)) + s_idx;
  474.  
  475.             /* Simple rule to find files linked "-x" */
  476.             only_ext = fh->rss == -1;
  477.             if (only_ext) {
  478.                 if (pr->isym == -1) {
  479.                     /* static function */
  480.                     sh = (SYMR*)-1;
  481.                 } else {
  482.                     /* external */
  483.                     name = hdr->cbExtOffset + pr->isym * sizeof(EXTR);
  484.                     sh = &((EXTR*)name)->asym;
  485.                 }
  486.             } else {
  487.                 /* Full symbols */
  488.                 sh = (SYMR*)fh->isymBase + pr->isym;
  489.                 /* Included code ? */
  490.                 if (s_idx == 0 && pr->adr != 0)
  491.                     code_offset -= pr->adr;
  492.             }
  493.  
  494.             /* Turn index into a pointer */
  495.             pr->isym = (long)sh;
  496.  
  497.             /* Fix line numbers */
  498.             pr->cbLineOffset += fh->cbLineOffset;
  499.  
  500.             /* Relocate address */
  501.             if (!only_ext)
  502.                 pr->adr += code_offset;
  503.         }        
  504.     }
  505.  
  506.     /* External symbols: fix string */
  507.     for (s_idx = 0; s_idx < hdr->iextMax; s_idx++) {
  508.         esh = (EXTR*)(hdr->cbExtOffset) + s_idx;
  509.         esh->asym.iss = esh->asym.iss + hdr->cbSsExtOffset;
  510.     }
  511. }
  512.  
  513.  
  514. /* Find a file descriptor given its index RF relative to a file CF */
  515.  
  516. static FDR *
  517. get_rfd (cf, rf)
  518.     int cf, rf;
  519. {
  520.     register FDR   *f;
  521.  
  522.     f = (FDR *) (cur_hdr->cbFdOffset) + cf;
  523.     /* Object files do not have the RFD table, all refs are absolute */
  524.     if (f->rfdBase == 0)
  525.         return (FDR *) (cur_hdr->cbFdOffset) + rf;
  526.     cf = *((pRFDT) f->rfdBase + rf);
  527.     return (FDR *) (cur_hdr->cbFdOffset) + cf;
  528. }
  529.  
  530. /* Return a safer print NAME for a file descriptor */
  531.  
  532. static char *
  533. fdr_name(name)
  534.     char *name;
  535. {
  536.     if (name == (char *) -1)
  537.         return "<stripped file>";
  538.     if (UNSAFE_DATA_ADDR(name))
  539.         return "<NFY>";
  540.     return name;
  541. }
  542.  
  543.  
  544. /* Read in and parse the symtab of the file DESC. INCREMENTAL says
  545.    whether we are adding to the general symtab or not. 
  546.    FIXME:  INCREMENTAL is currently always zero, though it should not be. */
  547.  
  548. static
  549. read_mips_symtab (abfd, desc)
  550.     bfd *abfd;
  551.     int desc;
  552. {
  553.     CORE_ADDR end_of_text_seg;
  554.  
  555.     read_the_mips_symtab(abfd, desc, &end_of_text_seg);
  556.  
  557.     parse_partial_symbols(end_of_text_seg);
  558.  
  559.     /*
  560.      * Check to make sure file was compiled with -g.
  561.      * If not, warn the user of this limitation.
  562.      */
  563.     if (compare_glevel(max_glevel, GLEVEL_2) < 0) {
  564.         if (max_gdbinfo == 0)
  565.             printf("\n%s not compiled with -g, debugging support is limited.", symfile);
  566.         printf("\nYou should compile with -g2 or -g3 for best debugging support.\n");
  567.         fflush(stdout);
  568.     }
  569. }
  570.  
  571. /* Local utilities */
  572.  
  573. /* Map of FDR indexes to partial symtabs */
  574.  
  575. static struct pst_map {
  576.     struct partial_symtab *pst;    /* the psymtab proper */
  577.     int n_globals;            /* globals it exports */
  578.     int n_statics;            /* statics (locals) it contains */
  579. } * fdr_to_pst;
  580.  
  581.  
  582. /* Utility stack, used to nest procedures and blocks properly.
  583.    It is a doubly linked list, to avoid too many alloc/free.
  584.    Since we might need it quite a few times it is NOT deallocated
  585.    after use. */
  586.  
  587. static struct parse_stack {
  588.     struct parse_stack    *next, *prev;
  589.     struct symtab        *cur_st;    /* Current symtab */
  590.     struct block        *cur_block;    /* Block in it */
  591.     int             blocktype;    /* What are we parsing */
  592.     int             maxsyms;    /* Max symbols in this block */
  593.     struct type        *cur_type;    /* Type we parse fields for */
  594.     int             procadr;    /* Start addres of this procedure */
  595.     int             numargs;    /* Its argument count */
  596. } *top_stack;    /* Top stack ptr */
  597.  
  598.  
  599. /* Enter a new lexical context */
  600.  
  601. static push_parse_stack()
  602. {
  603.     struct parse_stack *new;
  604.  
  605.     /* Reuse frames if possible */
  606.     if (top_stack && top_stack->prev)
  607.         new = top_stack->prev;
  608.     else
  609.         new = (struct parse_stack *) xzalloc(sizeof(struct parse_stack));
  610.     /* Initialize new frame with previous content */
  611.     if (top_stack) {
  612.         register struct parse_stack *prev = new->prev;
  613.  
  614.         *new = *top_stack;
  615.         top_stack->prev = new;
  616.         new->prev = prev;
  617.         new->next = top_stack;
  618.     }
  619.     top_stack = new;
  620. }
  621.  
  622. /* Exit a lexical context */
  623.  
  624. static pop_parse_stack()
  625. {
  626.     if (!top_stack)
  627.         return;
  628.     if (top_stack->next)
  629.         top_stack = top_stack->next;
  630. }
  631.  
  632.  
  633. /* Cross-references might be to things we haven't looked at
  634.    yet, e.g. type references.  To avoid too many type
  635.    duplications we keep a quick fixup table, an array
  636.    of lists of references indexed by file descriptor */
  637.  
  638. static struct pending {
  639.     struct pending    *next;        /* link */
  640.     SYMR        *s;        /* the symbol */
  641.     struct type    *t;        /* its partial type descriptor */
  642. } **pending_list;
  643.  
  644.  
  645. /* Check whether we already saw symbol SH in file FH as undefined */
  646.  
  647. static
  648. struct pending *is_pending_symbol(fh, sh)
  649.     FDR *fh;
  650.     SYMR *sh;
  651. {
  652.     int             f_idx = fh - (FDR *) cur_hdr->cbFdOffset;
  653.     register struct pending *p;
  654.  
  655.     /* Linear search is ok, list is typically no more than 10 deep */
  656.     for (p = pending_list[f_idx]; p; p = p->next)
  657.         if (p->s == sh)
  658.             break;
  659.     return p;
  660. }
  661.  
  662. /* Check whether we already saw type T in file FH as undefined */
  663.  
  664. static
  665. struct pending *is_pending_type(fh, t)
  666.     FDR *fh;
  667.     struct type *t;
  668. {
  669.     int             f_idx = fh - (FDR *) cur_hdr->cbFdOffset;
  670.     register struct pending *p;
  671.  
  672.     for (p = pending_list[f_idx]; p; p = p->next)
  673.         if (p->t == t)
  674.             break;
  675.     return p;
  676. }
  677.  
  678. /* Add a new undef symbol SH of type T */
  679.  
  680. static
  681. add_pending(fh, sh, t)
  682.     FDR *fh;
  683.     SYMR *sh;
  684.     struct type *t;
  685. {
  686.     int             f_idx = fh - (FDR *) cur_hdr->cbFdOffset;
  687.     struct pending *p = is_pending_symbol(fh, sh);
  688.  
  689.     /* Make sure we do not make duplicates */
  690.     if (!p) {
  691.         p = (struct pending *) xmalloc(sizeof(*p));
  692.         p->s = sh;
  693.         p->t = t;
  694.         p->next = pending_list[f_idx];
  695.         pending_list[f_idx] = p;
  696.     }
  697.     sh->reserved = 1;    /* for quick check */
  698. }
  699.  
  700. /* Throw away undef entries when done with file index F_IDX */
  701.  
  702. static
  703. free_pending(f_idx)
  704. {
  705.     register struct pending *p, *q;
  706.  
  707.     for (p = pending_list[f_idx]; p; p = q) {
  708.         q = p->next;
  709.         free(p);
  710.     }
  711.     pending_list[f_idx] = 0;
  712. }
  713.  
  714. /* The number of args to a procedure is not explicit in the symtab,
  715.    this is the list of all those we know of.
  716.    This makes parsing more reasonable and avoids extra passes */
  717.  
  718. static struct numarg {
  719.     struct numarg    *next;        /* link */
  720.     unsigned     adr;        /* procedure's start address */
  721.     unsigned     num;        /* arg count */
  722. } *numargs_list;
  723.  
  724. /* Record that the procedure at ADR takes NUM arguments. */
  725.  
  726. static
  727. got_numargs(adr,num)
  728. {
  729.     struct numarg  *n = (struct numarg *) xmalloc(sizeof(struct numarg));
  730.  
  731.     n->adr = adr;
  732.     n->num = num;
  733.     n->next = numargs_list;
  734.     numargs_list = n;
  735. }
  736.  
  737. /* See if we know how many arguments the procedure at ADR takes */
  738.  
  739. static
  740. lookup_numargs(adr)
  741. {
  742.     struct numarg  *n = numargs_list;
  743.  
  744.     while (n && n->adr != adr)
  745.         n = n->next;
  746.     return (n) ? n->num : -1;
  747. }
  748.  
  749. /* Release storage when done with this file */
  750.  
  751. static void
  752. free_numargs()
  753. {
  754.     struct numarg  *n = numargs_list, *m;
  755.  
  756.     while (n) {
  757.         m = n->next;
  758.         free(n);
  759.         n = m;
  760.     }
  761.     numargs_list = 0;
  762. }
  763.  
  764.  
  765. /* Parsing Routines proper. */
  766.  
  767. /* Parse a single symbol. Mostly just make up a GDB symbol for it.
  768.    For blocks, procedures and types we open a new lexical context.
  769.    This is basically just a big switch on the symbol's type */
  770.  
  771. static void
  772. parse_symbol(sh, ax)
  773.     SYMR *sh;
  774.     AUXU *ax;
  775. {
  776.     struct symbol  *s;
  777.     struct block   *b;
  778.     struct type    *t;
  779.     struct field   *f;
  780.     /* When a symbol is cross-referenced from other files/symbols
  781.        we mark it explicitly */
  782.     int             pend = (sh->reserved == 1);
  783.     enum address_class class;
  784.  
  785.     switch (sh->st) {
  786.  
  787.         case stNil:
  788.         break;
  789.  
  790.         case stGlobal:    /* external symbol, goes into global block */
  791.         class = LOC_STATIC;
  792.         b = BLOCKVECTOR_BLOCK(BLOCKVECTOR(top_stack->cur_st),
  793.                       GLOBAL_BLOCK);
  794.         s = new_symbol(sh->iss);
  795.         SYMBOL_VALUE_ADDRESS(s) = (CORE_ADDR)sh->value;
  796.         goto data;
  797.  
  798.         case stStatic:    /* static data, goes into current block. */
  799.         class = LOC_STATIC;
  800.         b = top_stack->cur_block;
  801.         s = new_symbol(sh->iss);
  802.         SYMBOL_VALUE_ADDRESS(s) = (CORE_ADDR)sh->value;
  803.         goto data;
  804.  
  805.         case stLocal:    /* local variable, goes into current block */
  806.         if (sh->sc == scRegister) {
  807.             class = LOC_REGISTER;
  808.             if (sh->value > 31)
  809.                 sh->value += 6;
  810.         } else
  811.             class = LOC_LOCAL;
  812.         b = top_stack->cur_block;
  813.         s = new_symbol(sh->iss);
  814.         SYMBOL_VALUE(s) = sh->value;
  815.  
  816. data:        /* Common code for symbols describing data */
  817.         SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
  818.         SYMBOL_CLASS(s) = class;
  819.         add_symbol(s, b);
  820.  
  821.         /* Type could be missing in a number of cases */
  822.         if (sh->sc == scUndefined || sh->sc == scNil ||
  823.             sh->index == 0xfffff)
  824.             SYMBOL_TYPE(s) = builtin_type_int;    /* undefined? */
  825.         else
  826.             SYMBOL_TYPE(s) = parse_type(ax + sh->index, sh, 0);
  827.         /* Value of a data symbol is its memory address */
  828.         break;
  829.  
  830.         case stParam:    /* arg to procedure, goes into current block */
  831.         max_gdbinfo++;
  832.         top_stack->numargs++;
  833.         s = new_symbol(sh->iss);
  834.         SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
  835.         if (sh->sc == scRegister) {
  836.             SYMBOL_CLASS(s) = LOC_REGPARM;
  837.             if (sh->value > 31)
  838.                 sh->value += 6;
  839.         } else
  840.             SYMBOL_CLASS(s) = LOC_ARG;
  841.         SYMBOL_VALUE(s) = sh->value;
  842.         SYMBOL_TYPE(s) = parse_type(ax + sh->index, sh, 0);
  843.         add_symbol(s, top_stack->cur_block);
  844.         break;
  845.  
  846.         case stLabel:    /* label, goes into current block */
  847.         s = new_symbol(sh->iss);
  848.         SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;    /* so that it can be used */
  849.         SYMBOL_CLASS(s) = LOC_LABEL;        /* but not misused */
  850.         SYMBOL_VALUE_ADDRESS(s) = (CORE_ADDR)sh->value;
  851.         SYMBOL_TYPE(s) = builtin_type_int;
  852.         add_symbol(s, top_stack->cur_block);
  853.         break;
  854.  
  855.         case stProc:    /* Procedure, goes into current block? FIXME */
  856.         case stStaticProc:    /* Static procedure, goes into current block */
  857.         s = new_symbol(sh->iss);
  858.         SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
  859.         SYMBOL_CLASS(s) = LOC_BLOCK;
  860.         /* Type of the return value */
  861.         if (sh->sc == scUndefined || sh->sc == scNil)
  862.             t = builtin_type_int;
  863.         else
  864.             t = parse_type(ax + sh->index, sh, 0);
  865.         add_symbol(s, top_stack->cur_block);
  866.  
  867.         /* Make a type for the procedure itself */
  868.         SYMBOL_TYPE(s) = lookup_function_type (t);
  869.  
  870.         /* Create and enter a new lexical context */
  871.         b = new_block(top_stack->maxsyms);
  872.         SYMBOL_BLOCK_VALUE(s) = b;
  873.         BLOCK_FUNCTION(b) = s;
  874.         BLOCK_START(b) = BLOCK_END(b) = sh->value;
  875.         BLOCK_SUPERBLOCK(b) = top_stack->cur_block;
  876.         add_block(b, top_stack->cur_st);
  877.  
  878.         /* Not if we only have partial info */
  879.         if (sh->sc == scUndefined || sh->sc == scNil)
  880.             break;
  881.  
  882.         push_parse_stack();
  883.         top_stack->cur_block = b;
  884.         top_stack->blocktype = sh->st;
  885.         top_stack->cur_type = SYMBOL_TYPE(s);
  886.         top_stack->procadr = sh->value;
  887.         top_stack->numargs = 0;
  888.  
  889.         sh->value = (long) SYMBOL_TYPE(s);
  890.         break;
  891.  
  892.         case stBlock:    /* Either a lexical block, or some type */
  893.         push_parse_stack();
  894.         top_stack->blocktype = stBlock;
  895.         if (sh->sc == scInfo) {    /* structure/union/enum def */
  896.             s = new_symbol(sh->iss);
  897.             SYMBOL_NAMESPACE(s) = STRUCT_NAMESPACE;
  898.             SYMBOL_CLASS(s) = LOC_TYPEDEF;
  899.             SYMBOL_VALUE(s) = 0;
  900.             add_symbol(s, top_stack->cur_block);
  901.             /* If this type was expected, use its partial definition */
  902.             if (pend) {
  903.                 t = is_pending_symbol(cur_fdr, sh)->t;
  904.             } else {
  905.                 /* Uhmm, can`t decide yet. Smash later */
  906.                 t = new_type(sh->iss);
  907.                 TYPE_CODE(t) = TYPE_CODE_UNDEF;
  908.                 add_pending(cur_fdr, sh, t);
  909.             }
  910.             SYMBOL_TYPE(s) = t;
  911.             /* make this the current type */
  912.             top_stack->cur_type = t;
  913.             TYPE_LENGTH(t) = sh->value;
  914.             /* Mark that symbol has a type, and say which one */
  915.             sh->value = (long) t;
  916.         } else {
  917.             /* beginnning of (code) block. Value of symbol
  918.                is the displacement from procedure start */
  919.             b = new_block(top_stack->maxsyms);
  920.             BLOCK_START(b) = sh->value + top_stack->procadr;
  921.             BLOCK_SUPERBLOCK(b) = top_stack->cur_block;
  922.             top_stack->cur_block = b;
  923.             add_block(b, top_stack->cur_st);
  924.         }
  925.         break;
  926.  
  927.         case stEnd:        /* end (of anything) */
  928.         if (sh->sc == scInfo) {
  929.             /* Finished with type */
  930.             top_stack->cur_type = 0;
  931.         } else if (sh->sc == scText &&
  932.                (top_stack->blocktype == stProc ||
  933.                 top_stack->blocktype == stStaticProc)) {
  934.             /* Finished with procedure */
  935.             struct blockvector *bv = BLOCKVECTOR(top_stack->cur_st);
  936.             struct block *b;
  937.             int i;
  938.  
  939.             BLOCK_END(top_stack->cur_block) += sh->value; /* size */
  940.             got_numargs(top_stack->procadr, top_stack->numargs);
  941.             /* Reallocate symbols, saving memory */
  942.             b = shrink_block(top_stack->cur_block, top_stack->cur_st);
  943.  
  944.             /* f77 emits proc-level with address bounds==[0,0],
  945.                So look for such child blocks, and patch them.  */
  946.             for (i = 0; i < BLOCKVECTOR_NBLOCKS(bv); i++) {
  947.             struct block *b_bad = BLOCKVECTOR_BLOCK(bv,i);
  948.             if (BLOCK_SUPERBLOCK(b_bad) == b
  949.              && BLOCK_START(b_bad) == top_stack->procadr
  950.              && BLOCK_END(b_bad) == top_stack->procadr) {
  951.                 BLOCK_START(b_bad) = BLOCK_START(b);
  952.                 BLOCK_END(b_bad) = BLOCK_END(b);
  953.             }
  954.             }
  955.             if (entry_point < BLOCK_END(b)
  956.               && entry_point >= BLOCK_START(b)) {
  957.             startup_file_start = BLOCK_START(b);
  958.             startup_file_end = BLOCK_END(b);
  959.             }
  960.         } else if (sh->sc == scText && top_stack->blocktype == stBlock) {
  961.             /* End of (code) block. The value of the symbol
  962.                is the displacement from the procedure`s start
  963.                address of the end of this block. */
  964.             BLOCK_END(top_stack->cur_block) = sh->value + top_stack->procadr;
  965.             (void) shrink_block(top_stack->cur_block, top_stack->cur_st);
  966.         }
  967.         pop_parse_stack();    /* restore previous lexical context */
  968.         break;
  969.  
  970.         case stMember:    /* member of struct/union/enum.. */
  971.         f = new_field(top_stack->cur_type, sh->iss);
  972.         f->bitpos = sh->value;
  973.         f->type = parse_type(ax + sh->index, sh, &f->bitsize);
  974.         break;
  975.  
  976.         case stTypedef:    /* type definition */
  977.         s = new_symbol(sh->iss);
  978.         SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
  979.         SYMBOL_CLASS(s) = LOC_TYPEDEF;
  980.         SYMBOL_BLOCK_VALUE(s) = top_stack->cur_block;
  981.         add_symbol(s, top_stack->cur_block);            
  982.         SYMBOL_TYPE(s) = parse_type(ax + sh->index, sh, 0);
  983.         sh->value = (long) SYMBOL_TYPE(s);
  984.         break;
  985.  
  986.         case stFile:    /* file name */
  987.         push_parse_stack();
  988.         top_stack->blocktype = sh->st;
  989.         break;
  990.  
  991.         /* I`ve never seen these for C */
  992.         case stRegReloc:
  993.         break;        /* register relocation */
  994.         case stForward:
  995.         break;        /* forwarding address */
  996.         case stConstant:
  997.         break;        /* constant */
  998.         default:
  999.         error("Unknown symbol type %x.", sh->st);
  1000.     }
  1001.     sh->st = stParsed;
  1002. }
  1003.  
  1004. /* Parse the type information provided in the AX entries for
  1005.    the symbol SH. Return the bitfield size in BS, in case. */
  1006.  
  1007. static struct type *parse_type(ax, sh, bs)
  1008.     AUXU    *ax;
  1009.     SYMR    *sh;
  1010.     int    *bs;
  1011. {
  1012.     /* Null entries in this map are treated specially */
  1013.     static struct type **map_bt[] =
  1014.     {
  1015.          &builtin_type_void,        /* btNil */
  1016.          0,                /* btAdr */
  1017.          &builtin_type_char,        /* btChar */
  1018.          &builtin_type_unsigned_char,    /* btUChar */
  1019.          &builtin_type_short,        /* btShort */
  1020.          &builtin_type_unsigned_short,    /* btUShort */
  1021.          &builtin_type_int,        /* btInt */
  1022.          &builtin_type_unsigned_int,    /* btUInt */
  1023.          &builtin_type_long,        /* btLong */
  1024.          &builtin_type_unsigned_long,    /* btULong */
  1025.          &builtin_type_float,        /* btFloat */
  1026.          &builtin_type_double,        /* btDouble */
  1027.          0,                /* btStruct */
  1028.          0,                /* btUnion */
  1029.          0,                /* btEnum */
  1030.          0,                /* btTypedef */
  1031.          0,                /* btRange */
  1032.          0,                /* btSet */
  1033.          &builtin_type_complex,        /* btComplex */
  1034.          &builtin_type_double_complex,    /* btDComplex */
  1035.          0,                /* btIndirect */
  1036.          &builtin_type_fixed_dec,    /* btFixedDec */
  1037.          &builtin_type_float_dec,    /* btFloatDec */
  1038.          &builtin_type_string,        /* btString */
  1039.          0,                /* btBit */
  1040.          0,                /* btPicture */
  1041.          &builtin_type_void,        /* btVoid */
  1042.     };
  1043.  
  1044.     TIR            *t;
  1045.     struct type    *tp = 0, *tp1;
  1046.     char           *fmt = "%s";
  1047.  
  1048.     /* Procedures start off by one */
  1049.     if (sh->st == stProc || sh->st == stStaticProc)
  1050.         ax++;
  1051.  
  1052.     /* Undefined ? Should not happen */
  1053.     if (ax->rndx.rfd == 0xfff) {
  1054.         return builtin_type_void;
  1055.     }
  1056.  
  1057.     /* Use aux as a type information record, map its basic type */
  1058.     t = &ax->ti;
  1059.     if (t->bt > 26 || t->bt == btPicture) {
  1060.         complain (&basic_type_complaint, t->bt);
  1061.         return builtin_type_int;
  1062.     }
  1063.     if (map_bt[t->bt])
  1064.         tp = *map_bt[t->bt];
  1065.     else {
  1066.         /* Cannot use builtin types, use templates */
  1067.         tp = make_type(TYPE_CODE_VOID, 0, 0, 0);
  1068.         switch (t->bt) {
  1069.             case btAdr:
  1070.             *tp = *builtin_type_ptr;
  1071.             break;
  1072.             case btStruct:
  1073.             *tp = *builtin_type_struct;
  1074.             fmt = "struct %s";
  1075.             break;
  1076.             case btUnion:
  1077.             *tp = *builtin_type_union;
  1078.             fmt = "union %s";
  1079.             break;
  1080.             case btEnum:
  1081.             *tp = *builtin_type_enum;
  1082.             fmt = "enum %s";
  1083.             break;
  1084.             case btRange:
  1085.             *tp = *builtin_type_range;
  1086.             break;
  1087.             case btSet:
  1088.             *tp = *builtin_type_set;
  1089.             fmt = "set %s";
  1090.             break;
  1091.         }
  1092.     }
  1093.  
  1094.     /* Move on to next aux */
  1095.     ax++;
  1096.     if (t->continued) {
  1097.         /* This is the way it would work if the compiler worked */
  1098.         register TIR *t1 = t;
  1099.         while (t1->continued)
  1100.             ax++;
  1101.     }
  1102.  
  1103.     /* For bitfields all we need is the width */
  1104.     if (t->fBitfield) {
  1105.         *bs = ax->width;
  1106.         return tp;
  1107.     }
  1108.  
  1109.     /* All these types really point to some (common) MIPS type
  1110.        definition, and only the type-qualifiers fully identify
  1111.        them.  We`ll make the same effort at sharing */
  1112.     if (t->bt == btIndirect ||
  1113.         t->bt == btStruct ||
  1114.         t->bt == btUnion ||
  1115.         t->bt == btEnum ||
  1116.         t->bt == btTypedef ||
  1117.         t->bt == btRange ||
  1118.         t->bt == btSet) {
  1119.         char            name[256], *pn;
  1120.  
  1121.         /* Try to cross reference this type */
  1122.         tp1 = tp;
  1123.         ax += cross_ref(ax, &tp1, &pn);
  1124.         /* SOMEONE OUGHT TO FIX DBXREAD TO DROP "STRUCT" */
  1125.         sprintf(name, fmt, pn);
  1126.  
  1127.         /* reading .o file ? */
  1128.         if (UNSAFE_DATA_ADDR(tp1))
  1129.             tp1 = tp;
  1130.         if (TYPE_CODE(tp1) == TYPE_CODE_UNDEF) {
  1131.             /*
  1132.              * Type was incompletely defined, now we know.
  1133.              */
  1134.             TYPE_CODE(tp1) = TYPE_CODE(tp);
  1135.             TYPE_NAME(tp1) = obsavestring(name, strlen(name));
  1136.             if (TYPE_CODE(tp1) == TYPE_CODE_ENUM) {
  1137.                 int             i;
  1138.  
  1139.                 for (i = 0; i < TYPE_NFIELDS(tp1); i++)
  1140.                     make_enum_constant(&TYPE_FIELD(tp1,i), tp1);
  1141.             }
  1142.         }
  1143.         if (tp1 != tp) {
  1144.             /* found as cross ref, rid of our template */
  1145.             if ((TYPE_FLAGS(tp) & TYPE_FLAG_PERM) == 0)
  1146.                 free(tp);
  1147.             tp = tp1;
  1148.             /* stupid idea of prepending "struct" to type names */
  1149.             if (t->bt == btStruct && !index(TYPE_NAME(tp), ' ')) {
  1150.                 sprintf(name, fmt, TYPE_NAME(tp));
  1151.                 TYPE_NAME(tp) = obsavestring(name, strlen(name));
  1152.             }
  1153.         } else
  1154.             TYPE_NAME(tp) = savestring(name, strlen(name));
  1155.     }
  1156.  
  1157.     /* Deal with range types */
  1158.     if (t->bt == btRange) {
  1159.         struct field   *f;
  1160.  
  1161.         f = new_field(tp, "Low");
  1162.         f->bitpos = ax->dnLow;
  1163.         ax++;
  1164.         f = new_field(tp, "High");
  1165.         f->bitpos = ax->dnHigh;
  1166.         ax++;
  1167.     }
  1168.  
  1169.     /* Parse all the type qualifiers now. If there are more
  1170.        than 6 the game will continue in the next aux */
  1171.  
  1172. #define PARSE_TQ(tq) \
  1173.     if (t->tq != tqNil) ax += upgrade_type(&tp, t->tq, ax, sh);
  1174.  
  1175. again:    PARSE_TQ(tq0);
  1176.     PARSE_TQ(tq1);
  1177.     PARSE_TQ(tq2);
  1178.     PARSE_TQ(tq3);
  1179.     PARSE_TQ(tq4);
  1180.     PARSE_TQ(tq5);
  1181. #undef    PARSE_TQ
  1182.  
  1183.     if (t->continued) {
  1184.         t++;
  1185.         goto again;
  1186.     }
  1187.     return tp;
  1188. }
  1189.  
  1190. /* Make up a complex type from a basic one.  Type is passed by
  1191.    reference in TPP and side-effected as necessary. The type
  1192.    qualifier TQ says how to handle the aux symbols at AX for
  1193.    the symbol SX we are currently analyzing.
  1194.    Returns the number of aux symbols we parsed. */
  1195.  
  1196. static int
  1197. upgrade_type(tpp, tq, ax, sh)
  1198.     struct type   **tpp;
  1199.     AUXU           *ax;
  1200.     SYMR           *sh;
  1201. {
  1202.     int             off;
  1203.     struct type    *t;
  1204.  
  1205.     /* Used in array processing */
  1206.     int             rf, id;
  1207.     FDR            *fh;
  1208.     struct field   *f;
  1209.     SYMR            ss;
  1210.     int        lower, upper;
  1211.  
  1212.     switch (tq) {
  1213.     case tqPtr:
  1214.         t = lookup_pointer_type (*tpp);
  1215.         *tpp = t;
  1216.         return 0;
  1217.  
  1218.     case tqProc:
  1219.         t = lookup_function_type (*tpp);
  1220.         *tpp = t;
  1221.         return 0;
  1222.  
  1223.     case tqArray:
  1224.         off = 0;
  1225.         t = make_type(TYPE_CODE_ARRAY, 0, 0, 0);
  1226.         TYPE_TARGET_TYPE(t) = *tpp;
  1227.  
  1228.         /* Determine and record the domain type (type of index) */
  1229.         id = ax->rndx.index;
  1230.         rf = ax->rndx.rfd;
  1231.         if (rf == 0xfff) {
  1232.             rf = (++ax)->isym;
  1233.             off++;
  1234.         }
  1235.         fh = get_rfd(cur_fd, rf);
  1236.         f = new_field(t, (char *)0);
  1237.         bzero(&ss, sizeof ss);
  1238. /* XXX */    f->type = parse_type(fh->iauxBase + id * sizeof(AUXU),
  1239.                      &ss, &f->bitsize);
  1240.  
  1241.         if (off == 0) {
  1242.         /*
  1243.          * This seems to be a pointer to the end of the Block defining
  1244.          * the type.  Why it is here is magic for me, and I have no
  1245.          * good use for it anyways.
  1246.          */
  1247.             /* This used to occur because cross_ref returned
  1248.                the wrong result (ax pointed wrong).  FIXME,
  1249.                delete this code in a while.  -- gnu@cygnus jul91 */
  1250.             complain (&array_parse_complaint, 0);
  1251.             off++;
  1252.             id = (++ax)->rndx.index;
  1253.             if ((rf = ax->rndx.rfd) == 0xfff)
  1254.                 rf = (++ax)->isym, off++;
  1255.         }
  1256.         lower = (++ax)->dnLow;
  1257.         upper = (++ax)->dnHigh;
  1258.         rf = (++ax)->width;    /* bit size of array element */
  1259.  
  1260.         /* Check whether supplied array element bit size matches
  1261.            the known size of the element type.  If this complaint
  1262.            ends up not happening, we can remove this code.  It's
  1263.            here because we aren't sure we understand this *&%&$
  1264.            symbol format.  */
  1265.         id = TYPE_LENGTH(TYPE_TARGET_TYPE(t)) << 3; /* bitsize */
  1266.         if (id == 0) {
  1267.             /* Most likely an undefined type */
  1268.             id = rf;
  1269.             TYPE_LENGTH(TYPE_TARGET_TYPE(t)) = id >> 3;
  1270.         }
  1271.         if (id != rf)
  1272.             complain (&array_bitsize_complaint, rf);
  1273.  
  1274.         TYPE_LENGTH(t) = (upper < 0) ? 0 :
  1275.             (upper - lower + 1) * (rf >> 3);
  1276.         *tpp = t;
  1277.         return 4 + off;
  1278.  
  1279.     case tqVol:
  1280.         /* Volatile -- currently ignored */
  1281.         return 0;
  1282.  
  1283.     default:
  1284.         complain (&unknown_type_qual_complaint, tq);
  1285.         return 0;
  1286.     }
  1287. }
  1288.  
  1289.  
  1290. /* Parse a procedure descriptor record PR.  Note that the procedure
  1291.    is parsed _after_ the local symbols, now we just make up the
  1292.    extra information we need into a special symbol that we insert
  1293.    in the procedure's main block.  Note also that images that
  1294.    have been partially stripped (ld -x) have been deprived
  1295.    of local symbols, and we have to cope with them here.
  1296.    The procedure's code ends at BOUND */
  1297.  
  1298. static
  1299. parse_procedure(pr, bound)
  1300.     PDR *pr;
  1301. {
  1302.     struct symbol *s, *i;
  1303.     SYMR *sh = (SYMR*)pr->isym;
  1304.     struct block *b;
  1305.     struct mips_extra_func_info *e;
  1306.     char name[100];
  1307.     char *sh_name;
  1308.  
  1309.     /* Reuse the MIPS record */
  1310.     e = (struct mips_extra_func_info *) pr;
  1311.     e->numargs = lookup_numargs(pr->adr);
  1312.  
  1313.     /* Make up our special symbol */
  1314.     i = new_symbol(".gdbinfo.");
  1315.     SYMBOL_VALUE(i) = (int)e;
  1316.     SYMBOL_NAMESPACE(i) = LABEL_NAMESPACE;
  1317.     SYMBOL_CLASS(i) = LOC_CONST;
  1318.     SYMBOL_TYPE(i) = builtin_type_void;
  1319.  
  1320.     /* Make up a name for static procedures. Sigh. */
  1321.     if (sh == (SYMR*)-1) {
  1322.         sprintf(name,".static_procedure@%x",pr->adr);
  1323.         sh_name = savestring(name, strlen(name));
  1324.         s = NULL;
  1325.     }
  1326.     else {
  1327.         sh_name = (char*)sh->iss;
  1328.         s = mylookup_symbol(sh_name, top_stack->cur_block,
  1329.                 VAR_NAMESPACE, LOC_BLOCK);
  1330.     }
  1331.     if (s != 0) {
  1332.         b = SYMBOL_BLOCK_VALUE(s);
  1333.     } else {
  1334.         s = new_symbol(sh_name);
  1335.         SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
  1336.         SYMBOL_CLASS(s) = LOC_BLOCK;
  1337.         /* Donno its type, hope int is ok */
  1338.         SYMBOL_TYPE(s) = lookup_function_type (builtin_type_int);
  1339.         add_symbol(s, top_stack->cur_block);
  1340.         /* Wont have symbols for this one */
  1341.         b = new_block(2);
  1342.         SYMBOL_BLOCK_VALUE(s) = b;
  1343.         BLOCK_FUNCTION(b) = s;
  1344.         BLOCK_START(b) = pr->adr;
  1345.         BLOCK_END(b) = bound;
  1346.         BLOCK_SUPERBLOCK(b) = top_stack->cur_block;
  1347.         add_block(b, top_stack->cur_st);
  1348.     }
  1349.     e->isym = (long)s;
  1350.     add_symbol(i,b);
  1351. }
  1352.  
  1353. /* Parse the external symbol ES. Just call parse_symbol() after
  1354.    making sure we know where the aux are for it. For procedures,
  1355.    parsing of the PDRs has already provided all the needed
  1356.    information, we only parse them if SKIP_PROCEDURES is false,
  1357.    and only if this causes no symbol duplication.
  1358.  
  1359.    This routine clobbers top_stack->cur_block and ->cur_st. */
  1360.  
  1361. static
  1362. parse_external(es, skip_procedures)
  1363.     EXTR *es;
  1364. {
  1365.     AUXU *ax;
  1366.  
  1367.     if (es->ifd != ifdNil) {
  1368.         cur_fd = es->ifd;
  1369.         cur_fdr = (FDR*)(cur_hdr->cbFdOffset) + cur_fd;
  1370.         ax = (AUXU*)cur_fdr->iauxBase;
  1371.     } else {
  1372.         cur_fdr = (FDR*)(cur_hdr->cbFdOffset);
  1373.         ax = 0;
  1374.     }
  1375.     top_stack->cur_st = cur_stab;
  1376.     top_stack->cur_block = BLOCKVECTOR_BLOCK(BLOCKVECTOR(top_stack->cur_st),
  1377.                          GLOBAL_BLOCK);
  1378.  
  1379.     /* Reading .o files */
  1380.     if (es->asym.sc == scUndefined || es->asym.sc == scNil) {
  1381.         char *what;
  1382.         switch (es->asym.st) {
  1383.         case stStaticProc:
  1384.         case stProc:    what = "procedure"; n_undef_procs++;  break;
  1385.         case stGlobal:    what = "variable";  n_undef_vars++;   break;
  1386.         case stLabel:    what = "label";     n_undef_labels++; break;
  1387.         default :    what = "symbol";                      break;
  1388.         }
  1389.         n_undef_symbols++;
  1390.         if (info_verbose)
  1391.             printf_filtered("Warning: %s `%s' is undefined (in %s)\n", what,
  1392.                 es->asym.iss, fdr_name(cur_fdr->rss));
  1393.         return;
  1394.     }
  1395.  
  1396.     switch (es->asym.st) {
  1397.     case stProc:
  1398.         /* If we have full symbols we do not need more */
  1399.         if (skip_procedures)
  1400.             return;
  1401.         if (mylookup_symbol (es->asym.iss, top_stack->cur_block,
  1402.                     VAR_NAMESPACE, LOC_BLOCK))
  1403.             break;
  1404.         /* fall through */
  1405.     case stGlobal:
  1406.     case stLabel:
  1407.         /*
  1408.          * Note that the case of a symbol with indexNil
  1409.          * must be handled anyways by parse_symbol().
  1410.          */
  1411.         parse_symbol(&es->asym, ax);
  1412.         break;
  1413.     default:
  1414.         break;
  1415.     }
  1416. }
  1417.  
  1418. /* Parse the line number info for file descriptor FH into
  1419.    GDB's linetable LT.  MIPS' encoding requires a little bit
  1420.    of magic to get things out.  Note also that MIPS' line
  1421.    numbers can go back and forth, apparently we can live
  1422.    with that and do not need to reorder our linetables */
  1423.  
  1424. static
  1425. parse_lines(fh, lt)
  1426.     FDR *fh;
  1427.     struct linetable *lt;
  1428. {
  1429.     char *base = (char*)fh->cbLineOffset;
  1430.     int i, j, k;
  1431.     int delta, count, lineno = 0;
  1432.     PDR *pr;
  1433.  
  1434.     if (base == 0)
  1435.         return;
  1436.  
  1437.     /* Scan by procedure descriptors */
  1438.     i = 0; j = 0, k = 0;
  1439.     for (pr = (PDR*)IPDFIRST(cur_hdr,fh); j < fh->cpd; j++, pr++) {
  1440.         int l, halt;
  1441.  
  1442.         /* No code for this one */
  1443.         if (pr->iline == ilineNil ||
  1444.             pr->lnLow == -1 || pr->lnHigh == -1)
  1445.             continue;
  1446.         /*
  1447.          *    Aurgh! To know where to stop expanding we
  1448.          *    must look-ahead.
  1449.          */
  1450.         for (l = 1; l < (fh->cpd - j); l++)
  1451.             if (pr[l].iline != -1)
  1452.                 break;
  1453.         if (l == (fh->cpd - j))
  1454.             halt = fh->cline;
  1455.         else
  1456.             halt = pr[l].iline;
  1457.         /*
  1458.          * When procedures are moved around the linenumbers
  1459.          * are attributed to the next procedure up
  1460.          */
  1461.         if (pr->iline >= halt) continue;
  1462.  
  1463.         base = (char*)pr->cbLineOffset;
  1464.         l = pr->adr >> 2;    /* in words */
  1465.         halt += (pr->adr >> 2) - pr->iline;
  1466.         for (lineno = pr->lnLow; l < halt;) {
  1467.             count = *base & 0x0f;
  1468.             delta = *base++ >> 4;
  1469.             if (delta == -8) {
  1470.                 delta = (base[0] << 8) | (base[1] & 0xff);
  1471.                 base += 2;
  1472.             }
  1473.             lineno += delta;/* first delta is 0 */
  1474.             k = add_line(lt, lineno, l, k);
  1475.             l += count + 1;
  1476.         }
  1477.     }
  1478. }
  1479.  
  1480.  
  1481. /* Parse the symbols of the file described by FH, whose index is F_IDX.
  1482.    BOUND is the highest core address of this file's procedures */
  1483.  
  1484. static
  1485. parse_one_file(fh, f_idx, bound)
  1486.     FDR *fh;
  1487. {
  1488.     register int s_idx;
  1489.     SYMR *sh;
  1490.     PDR *pr;
  1491.  
  1492.     /* Parse local symbols first */
  1493.  
  1494.     for (s_idx = 0; s_idx < fh->csym; s_idx++) {
  1495.         sh = (SYMR *) (fh->isymBase) + s_idx;
  1496.         cur_sdx = s_idx;
  1497.         parse_symbol(sh, fh->iauxBase);
  1498.     }
  1499.  
  1500.     /* Procedures next, note we need to look-ahead to
  1501.        find out where the procedure's code ends */
  1502.  
  1503.     for (s_idx = 0; s_idx < fh->cpd-1; s_idx++) {
  1504.         pr = (PDR *) (IPDFIRST(cur_hdr, fh)) + s_idx;
  1505.         parse_procedure(pr, pr[1].adr);    /* next proc up */
  1506.     }
  1507.     if (fh->cpd) {
  1508.         pr = (PDR *) (IPDFIRST(cur_hdr, fh)) + s_idx;
  1509.         parse_procedure(pr, bound);    /* next file up */
  1510.     }
  1511.  
  1512.     /* Linenumbers. At the end, check if we can save memory */
  1513.     parse_lines(fh, LINETABLE(cur_stab));
  1514.     if (LINETABLE(cur_stab)->nitems < fh->cline)
  1515.         shrink_linetable(cur_stab);
  1516. }
  1517.  
  1518. /* Master parsing procedure for first-pass reading of file symbols
  1519.    into a partial_symtab.
  1520.  
  1521.    Parses the symtab described by the global symbolic header CUR_HDR.
  1522.    END_OF_TEXT_SEG gives the address just after the text segment for
  1523.    the symtab we are reading.  */
  1524.  
  1525. static
  1526. parse_partial_symbols(end_of_text_seg)
  1527.     int end_of_text_seg;
  1528. {
  1529.     int             f_idx, s_idx, h_max, stat_idx;
  1530.     CORE_ADDR    dummy, *prevhigh;
  1531.     HDRR        *hdr;
  1532.     /* Running pointers */
  1533.     FDR        *fh;
  1534.     RFDT        *rh;
  1535.     register EXTR    *esh;
  1536.     register SYMR    *sh;
  1537.     struct partial_symtab *pst;
  1538.  
  1539.     /*
  1540.      * Big plan: 
  1541.      *
  1542.      * Only parse the Local and External symbols, and the Relative FDR.
  1543.      * Fixup enough of the loader symtab to be able to use it.
  1544.      * Allocate space only for the file`s portions we need to
  1545.      * look at. (XXX)
  1546.      */
  1547.  
  1548.     hdr = cur_hdr;
  1549.     max_gdbinfo = 0;
  1550.     max_glevel = MIN_GLEVEL;
  1551.  
  1552.     /* Allocate the map FDR -> PST.
  1553.        Minor hack: -O3 images might claim some global data belongs
  1554.        to FDR -1. We`ll go along with that */
  1555.     fdr_to_pst = (struct pst_map *)xzalloc((hdr->ifdMax+1) * sizeof *fdr_to_pst);
  1556.     fdr_to_pst++;
  1557.     {
  1558.         struct partial_symtab * pst = new_psymtab("");
  1559.         fdr_to_pst[-1].pst = pst;
  1560.         pst->ldsymoff = -1;
  1561.     }
  1562.  
  1563.     /* Now scan the FDRs, mostly for dependencies */
  1564.     for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++)
  1565.         (void) parse_fdr(f_idx, 1);
  1566.  
  1567.     /* Take a good guess at how many symbols we might ever need */
  1568.     h_max = hdr->iextMax;
  1569.  
  1570.     /* Parse externals: two passes because they can be ordered
  1571.        in any way, but gdb likes to have them segregated by their
  1572.        source file.  */
  1573.  
  1574.     /* Pass 1 over external syms: Presize and partition the list */
  1575.     for (s_idx = 0; s_idx < hdr->iextMax; s_idx++) {
  1576.         esh = (EXTR *) (hdr->cbExtOffset) + s_idx;
  1577.         fdr_to_pst[esh->ifd].n_globals++;
  1578.     }
  1579.  
  1580.     if (global_psymbols.list) {
  1581.         int origsize = global_psymbols.next - global_psymbols.list;
  1582.  
  1583.         global_psymbols.list = (struct partial_symbol *)
  1584.             xrealloc (global_psymbols.list,
  1585.                (h_max + origsize) * sizeof(struct partial_symbol));
  1586.         global_psymbols.next = global_psymbols.list + origsize;
  1587.         global_psymbols.size = h_max + origsize;
  1588.     } else {
  1589.         global_psymbols.list = (struct partial_symbol *)
  1590.                 xmalloc (h_max * sizeof(struct partial_symbol));
  1591.         global_psymbols.next = global_psymbols.list;
  1592.         global_psymbols.size = h_max;
  1593.     }
  1594.  
  1595.     /* Pass 1.5 over files:  partition out global symbol space */
  1596.     s_idx    = global_psymbols.next - global_psymbols.list;
  1597.     for (f_idx = -1; f_idx < hdr->ifdMax; f_idx++) {
  1598.         fdr_to_pst[f_idx].pst->globals_offset = s_idx;
  1599.         s_idx += fdr_to_pst[f_idx].n_globals;
  1600.     }
  1601.  
  1602.     /* Pass 1.6 over files:  partition out static symbol space.
  1603.        Note that this loop starts at 0, not at -1. */
  1604.     stat_idx = static_psymbols.next - static_psymbols.list;
  1605.     for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) {
  1606.         fdr_to_pst[f_idx].pst->statics_offset = stat_idx;
  1607.         fh = f_idx + (FDR *)(hdr->cbFdOffset);
  1608.         stat_idx += fh->csym;
  1609.     }
  1610.  
  1611.     /* Now that we know its max size, allocate static symbol list */
  1612.     if (static_psymbols.list) {
  1613.         int origsize = static_psymbols.next - static_psymbols.list;
  1614.  
  1615.         static_psymbols.list = (struct partial_symbol *)
  1616.             xrealloc (static_psymbols.list,
  1617.                stat_idx * sizeof(struct partial_symbol));
  1618.         static_psymbols.next = static_psymbols.list + origsize;
  1619.         static_psymbols.size = stat_idx;
  1620.     } else {
  1621.         static_psymbols.list = (struct partial_symbol *)
  1622.             xmalloc (stat_idx * sizeof(struct partial_symbol));
  1623.         static_psymbols.next = static_psymbols.list;
  1624.         static_psymbols.size = stat_idx;
  1625.     }
  1626.  
  1627.     /* Pass 2 over external syms: fill in external symbols */
  1628.     for (s_idx = 0; s_idx < hdr->iextMax; s_idx++) {
  1629.         register struct partial_symbol *p;
  1630.         enum misc_function_type misc_type = mf_text;
  1631.         esh = (EXTR *) (hdr->cbExtOffset) + s_idx;
  1632.  
  1633.         if (esh->asym.sc == scUndefined || esh->asym.sc == scNil)
  1634.             continue;
  1635.  
  1636.         /* Locate the psymtab and the preallocated psymbol.  */
  1637.         pst = fdr_to_pst[esh->ifd].pst;
  1638.         p = global_psymbols.list + pst->globals_offset +
  1639.              pst->n_global_syms++;
  1640.         SYMBOL_NAME(p) = (char *)(esh->asym.iss);
  1641.         SYMBOL_NAMESPACE(p) = VAR_NAMESPACE;
  1642.  
  1643.         switch (esh->asym.st) {
  1644.         case stProc:
  1645.             SYMBOL_CLASS(p) = LOC_BLOCK;
  1646.             SYMBOL_VALUE(p) = esh->asym.value;
  1647.             break;
  1648.         case stGlobal:
  1649.             SYMBOL_CLASS(p) = LOC_STATIC;
  1650.             SYMBOL_VALUE_ADDRESS(p) = (CORE_ADDR)esh->asym.value;
  1651.             misc_type = mf_data;
  1652.             break;
  1653.         case stLabel:
  1654.             SYMBOL_CLASS(p) = LOC_LABEL;
  1655.             SYMBOL_VALUE_ADDRESS(p) = (CORE_ADDR)esh->asym.value;
  1656.             break;
  1657.         default:
  1658.             misc_type = mf_unknown;
  1659.             complain (&unknown_ext_complaint, SYMBOL_NAME(p));
  1660.         }
  1661.         prim_record_misc_function (SYMBOL_NAME(p),
  1662.                            SYMBOL_VALUE(p),
  1663.                            misc_type);
  1664.     }
  1665.  
  1666.     /* Pass 3 over files, over local syms: fill in static symbols */
  1667.     for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) {
  1668.         fh = f_idx + (FDR *)(cur_hdr->cbFdOffset);
  1669.         pst = fdr_to_pst[f_idx].pst;
  1670.         
  1671.         for (s_idx = 0; s_idx < fh->csym; ) {
  1672.             register struct partial_symbol *p;
  1673.  
  1674.             sh = s_idx + (SYMR *) fh->isymBase;
  1675.  
  1676.             if (sh->sc == scUndefined || sh->sc == scNil) {
  1677.                 /* FIXME, premature? */
  1678.                 s_idx++;
  1679.                 continue;
  1680.             }
  1681.  
  1682.             /* Locate the preallocated psymbol.  */
  1683.             p = static_psymbols.list + pst->statics_offset +
  1684.                  pst->n_static_syms;
  1685.             SYMBOL_NAME(p) = (char *)(sh->iss);
  1686.             SYMBOL_VALUE(p) = sh->value;
  1687.             SYMBOL_NAMESPACE(p) = VAR_NAMESPACE;
  1688.  
  1689.             switch (sh->st) {
  1690.             case stProc:        /* Asm labels apparently */
  1691.             case stStaticProc:        /* Function */
  1692.                 SYMBOL_CLASS(p) = LOC_BLOCK;
  1693.                 pst->n_static_syms++;    /* Use gdb symbol */
  1694.                 /* Skip over procedure to next one. */
  1695.                 s_idx = (sh->index + (AUXU *)fh->iauxBase)
  1696.                       ->isym;
  1697.                 continue;
  1698.             case stStatic:            /* Variable */
  1699.                 SYMBOL_CLASS(p) = LOC_STATIC;
  1700.                 SYMBOL_VALUE_ADDRESS(p) = (CORE_ADDR)sh->value;
  1701.                 break;
  1702.             case stTypedef:            /* Typedef */
  1703.                 SYMBOL_CLASS(p) = LOC_TYPEDEF;
  1704.                 break;
  1705.             case stConstant:        /* Constant decl */
  1706.                 SYMBOL_CLASS(p) = LOC_CONST;
  1707.                 break;
  1708.             case stBlock:            /* { }, str, un, enum */
  1709.                 /* Eventually we want struct names and enum
  1710.                    values out of here.  FIXME */
  1711.                 /* Skip over the block */
  1712.                 s_idx = sh->index;
  1713.                 continue;
  1714.             case stFile:            /* File headers */
  1715.             case stLabel:            /* Labels */
  1716.             case stEnd:            /* Ends of files */
  1717.                 goto skip;
  1718.             default:
  1719.                 complain (&unknown_sym_complaint, SYMBOL_NAME(p));
  1720.                 complain (&unknown_st_complaint, sh->st);
  1721.                 s_idx++;
  1722.                 continue;
  1723.             }
  1724.             pst->n_static_syms++;    /* Use this gdb symbol */
  1725.         skip:
  1726.             s_idx++;        /* Go to next file symbol */
  1727. #if 0
  1728. /* We don't usually record static syms, but some we seem to.  chk dbxread. */
  1729. /*FIXME*/        prim_record_misc_function (SYMBOL_NAME(p),
  1730.                            SYMBOL_VALUE(p),
  1731.                            misc_type);
  1732. #endif
  1733.         }
  1734.     }
  1735.  
  1736.     /* The array (of lists) of globals must be sorted.
  1737.        Take care, since we are at it, of pst->texthigh.
  1738.  
  1739.        NOTE: The way we handle textlow/high is incorrect, but good
  1740.        enough for a first approximation. The case we fail is on a
  1741.        file "foo.c" that looks like
  1742.            proc1() {...}
  1743.         #include "bar.c"    -- this contains proc2()
  1744.         proc3() {...}
  1745.        where proc3() is attributed to bar.c.  But since this is a
  1746.        dependent file it will cause loading of foo.c as well, so
  1747.        everything will be fine at the end.  */
  1748.  
  1749.     /* First, sort the psymtabs by their textlow addresses.  */
  1750.     reorder_psymtabs();
  1751.  
  1752.     /* Now, rip through and fill in "texthigh" from the textlow
  1753.        of the following psymtab.  Slimy but it might work.
  1754.        Sort the global psymbols while we're at it.  */
  1755.     prevhigh = &dummy;
  1756.     for (f_idx = 0; f_idx < hdr->ifdMax; f_idx++) {
  1757.         struct partial_symtab *pst = fdr_to_pst[f_idx].pst;
  1758.         if (pst->n_global_syms > 1)
  1759.             qsort (global_psymbols.list + pst->globals_offset,
  1760.                 pst->n_global_syms, sizeof (struct partial_symbol),
  1761.                 compare_psymbols);
  1762.         if (pst->textlow) {
  1763.             *prevhigh = pst->textlow;
  1764.             prevhigh = &pst->texthigh;
  1765.         }
  1766.     }
  1767.  
  1768.     /* Mark the last code address, and remember it for later */
  1769.     *prevhigh = end_of_text_seg;
  1770.     hdr->cbDnOffset = end_of_text_seg;
  1771.  
  1772.     free(&fdr_to_pst[-1]);
  1773.     fdr_to_pst = 0;
  1774. }
  1775.  
  1776.  
  1777. /* Do the initial analisys of the F_IDX-th file descriptor.
  1778.    Allocates a partial symtab for it, and builds the list
  1779.    of dependent files by recursion. LEV says at which level
  1780.    of recursion we are called (to pretty up debug traces) */
  1781.  
  1782. static struct partial_symtab *
  1783. parse_fdr(f_idx, lev)
  1784.     int f_idx;
  1785. {
  1786.     register FDR *fh;
  1787.     register struct partial_symtab *pst;
  1788.     int s_idx, s_id0;
  1789.  
  1790.     fh = (FDR *) (cur_hdr->cbFdOffset) + f_idx;
  1791.  
  1792.     /* Use this to indicate into which symtab this file was parsed */
  1793.     if (fh->ioptBase)
  1794.         return (struct partial_symtab *) fh->ioptBase;
  1795.  
  1796.     /* Debuggability level */
  1797.     if (compare_glevel(max_glevel, fh->glevel) < 0)
  1798.         max_glevel = fh->glevel;
  1799.  
  1800.     /* Make a new partial_symtab */
  1801.     pst = new_psymtab(fh->rss);
  1802.     if (fh->cpd == 0){
  1803.         pst->textlow = 0;
  1804.         pst->texthigh = 0;
  1805.     } else {
  1806.         pst->textlow = fh->adr;
  1807.         pst->texthigh = fh->cpd;    /* To be fixed later */
  1808.     }
  1809.  
  1810.     /* Make everything point to everything. */
  1811.     pst->ldsymoff = f_idx;
  1812.     fdr_to_pst[f_idx].pst = pst;
  1813.     fh->ioptBase = (int)pst;
  1814.  
  1815.     /* Analyze its dependencies */
  1816.     if (fh->crfd <= 1)
  1817.         return pst;
  1818.  
  1819.     s_id0 = 0;
  1820.     if (fh->cpd == 0) {  /* If there are no functions defined here ... */
  1821.         /* ...then presumably a .h file: drop reverse depends .h->.c */
  1822.         for (; s_id0 < fh->crfd; s_id0++) {
  1823.             RFDT *rh = (RFDT *) (fh->rfdBase) + s_id0;
  1824.             if (*rh == f_idx) {
  1825.                 s_id0++;    /* Skip self-dependency */
  1826.                 break;
  1827.             }
  1828.         }
  1829.     }
  1830.     pst->number_of_dependencies = fh->crfd - s_id0;
  1831.     pst->dependencies = (struct partial_symtab **)
  1832.         obstack_alloc (psymbol_obstack,
  1833.                  pst->number_of_dependencies *
  1834.                    sizeof (struct partial_symtab *));
  1835.     for (s_idx = s_id0; s_idx < fh->crfd; s_idx++) {
  1836.         RFDT *rh = (RFDT *) (fh->rfdBase) + s_idx;
  1837.  
  1838.         pst->dependencies[s_idx-s_id0] = parse_fdr(*rh, lev+1);
  1839.     }
  1840.  
  1841.     return pst;
  1842. }
  1843.  
  1844.  
  1845. /* Ancillary function to psymtab_to_symtab().  Does all the work
  1846.    for turning the partial symtab PST into a symtab, recurring
  1847.    first on all dependent psymtabs.  The argument FILENAME is
  1848.    only passed so we can see in debug stack traces what file
  1849.    is being read. */
  1850.  
  1851. static void
  1852. psymtab_to_symtab_1(pst, filename)
  1853.     struct partial_symtab *pst;
  1854.     char *filename;
  1855. {
  1856.     int             i, f_max;
  1857.     struct symtab  *st;
  1858.     FDR *fh;
  1859.  
  1860.     if (pst->readin)
  1861.         return;
  1862.     pst->readin = 1;
  1863.  
  1864.     pending_list = (struct pending **) cur_hdr->cbOptOffset;
  1865.     if (pending_list == 0) {
  1866.         pending_list = (struct pending **)
  1867.             xzalloc(cur_hdr->ifdMax * sizeof(struct pending *));
  1868.         cur_hdr->cbOptOffset = (int)pending_list;
  1869.     }
  1870.  
  1871.     /* How many symbols will we need */
  1872.     /* FIXME, this does not count enum values. */
  1873.     f_max = pst->n_global_syms + pst->n_static_syms;
  1874.     if (pst->ldsymoff == -1) {
  1875.         fh = 0;
  1876.         st = new_symtab( "unknown", f_max, 0);
  1877.     } else {
  1878.         fh = (FDR *) (cur_hdr->cbFdOffset) + pst->ldsymoff;
  1879.         f_max += fh->csym + fh->cpd;
  1880.         st = new_symtab(pst->filename, 2 * f_max, 2 * fh->cline);
  1881.     }
  1882.  
  1883.     /* Read in all partial symbtabs on which this one is dependent.
  1884.        NOTE that we do have circular dependencies, sigh.  We solved
  1885.        that by setting pst->readin before this point.  */
  1886.  
  1887.     for (i = 0; i < pst->number_of_dependencies; i++)
  1888.         if (!pst->dependencies[i]->readin) {
  1889.             /* Inform about additional files to be read in.  */
  1890.             if (info_verbose)
  1891.               {
  1892.                 fputs_filtered (" ", stdout);
  1893.                 wrap_here ("");
  1894.                 fputs_filtered ("and ", stdout);
  1895.                 wrap_here ("");
  1896.                 printf_filtered ("%s...",
  1897.                          pst->dependencies[i]->filename);
  1898.                 wrap_here ("");        /* Flush output */
  1899.                 fflush (stdout);
  1900.               }
  1901.             /* We only pass the filename for debug purposes */
  1902.             psymtab_to_symtab_1(pst->dependencies[i], 
  1903.                 pst->dependencies[i]->filename);
  1904.         }
  1905.  
  1906.     /* Now read the symbols for this symtab */
  1907.  
  1908.     cur_fd = pst->ldsymoff;
  1909.     cur_fdr = fh;
  1910.     cur_stab = st;
  1911.  
  1912.     /* Get a new lexical context */
  1913.  
  1914.     push_parse_stack();
  1915.     top_stack->cur_st = cur_stab;
  1916.     top_stack->cur_block = BLOCKVECTOR_BLOCK(BLOCKVECTOR(cur_stab),
  1917.                          STATIC_BLOCK);
  1918.     BLOCK_START(top_stack->cur_block) = fh ? fh->adr : 0;
  1919.     BLOCK_END(top_stack->cur_block) = 0;
  1920.     top_stack->blocktype = stFile;
  1921.     top_stack->maxsyms = 2*f_max;
  1922.     top_stack->cur_type = 0;
  1923.     top_stack->procadr = 0;
  1924.     top_stack->numargs = 0;
  1925.  
  1926.     /* Parse locals and procedures */
  1927.     if (fh)
  1928.         parse_one_file(fh, cur_fd, (cur_fd == (cur_hdr->ifdMax - 1)) ?
  1929.                 cur_hdr->cbDnOffset : fh[1].adr);
  1930.  
  1931.     /* .. and our share of externals.
  1932.        XXX use the global list to speed up things here. how ? 
  1933.        FIXME, Maybe quit once we have found the right number of ext's? */
  1934.     /* parse_external clobbers top_stack->cur_block and ->cur_st here. */
  1935.     top_stack->blocktype = stFile;
  1936.     top_stack->maxsyms = cur_hdr->isymMax + cur_hdr->ipdMax + cur_hdr->iextMax;
  1937.     for (i = 0; i < cur_hdr->iextMax; i++) {
  1938.         register EXTR *esh = (EXTR *) (cur_hdr->cbExtOffset) + i;
  1939.         if (esh->ifd == cur_fd)
  1940.             parse_external(esh, 1);
  1941.     }
  1942.  
  1943.     /* If there are undefined, tell the user */
  1944.     if (n_undef_symbols) {
  1945.         printf_filtered("File %s contains %d unresolved references:",
  1946.                 st->filename, n_undef_symbols);
  1947.         printf_filtered("\n\t%4d variables\n\t%4d procedures\n\t%4d labels\n",
  1948.                 n_undef_vars, n_undef_procs, n_undef_labels);
  1949.         n_undef_symbols = n_undef_labels = n_undef_vars = n_undef_procs = 0;
  1950.     }
  1951.  
  1952.     pop_parse_stack();
  1953.  
  1954.     /*
  1955.      * Sort the symbol table now, we are done adding symbols to it.
  1956.      */
  1957.     sort_symtab_syms(st);
  1958.  
  1959.     /* Now link the psymtab and the symtab.  */
  1960.     pst->symtab = st;
  1961. }
  1962.  
  1963. /* Ancillary parsing procedures. */
  1964.  
  1965. /* Lookup the type at relative index RN.  Return it in TPP
  1966.    if found and in any event come up with its name PNAME.
  1967.    Return value says how many aux symbols we ate */
  1968.  
  1969. static
  1970. cross_ref(rn, tpp, pname)
  1971.     RNDXR *rn;
  1972.     struct type **tpp;
  1973.     char **pname;
  1974. {
  1975.     unsigned        rf;
  1976.  
  1977.     /* Escape index means 'the next one' */
  1978.     if (rn->rfd == 0xfff)
  1979.         rf = *(unsigned *) (rn + 1);
  1980.     else
  1981.         rf = rn->rfd;
  1982.  
  1983.     if (rf == -1) {
  1984.         /* Ooops */
  1985.         *pname = "<undefined>";
  1986.     } else {
  1987.         /*
  1988.          * Find the relative file descriptor and the symbol in it 
  1989.          */
  1990.         FDR            *fh = get_rfd(cur_fd, rf);
  1991.         SYMR           *sh;
  1992.         struct type    *t;
  1993.  
  1994.         /*
  1995.          * If we have processed this symbol then we left a forwarding
  1996.          * pointer to the corresponding GDB symbol.  If not, we`ll put
  1997.          * it in a list of pending symbols, to be processed later when
  1998.          * the file f will be.  In any event, we collect the name for
  1999.          * the type here. Which is why we made a first pass at
  2000.          * strings. 
  2001.          */
  2002.         sh = (SYMR *) (fh->isymBase) + rn->index;
  2003.  
  2004.         /* Careful, we might be looking at .o files */
  2005.         *pname = (UNSAFE_DATA_ADDR(sh->iss)) ? "<undefined>" :
  2006.             (char *) sh->iss;
  2007.  
  2008.         /* Have we parsed it ? */
  2009.         if ((!UNSAFE_DATA_ADDR(sh->value)) && (sh->st == stParsed)) {
  2010.             t = (struct type *) sh->value;
  2011.             *tpp = t;
  2012.         } else {
  2013.             struct pending *p;
  2014.  
  2015.             /* Avoid duplicates */
  2016.             p = is_pending_symbol(fh, sh);
  2017.  
  2018.             if (p)
  2019.                 *tpp = p->t;
  2020.             else
  2021.                 add_pending(fh, sh, *tpp);
  2022.         }
  2023.     }
  2024.  
  2025.     /* We used one auxent normally, two if we got a "next one" rf. */
  2026.     return (rn->rfd == 0xfff? 2: 1);
  2027. }
  2028.  
  2029.  
  2030. /* Quick&dirty lookup procedure, to avoid the MI ones that require
  2031.    keeping the symtab sorted */
  2032.  
  2033. static struct symbol *
  2034. mylookup_symbol (name, block, namespace, class)
  2035.      char *name;
  2036.      register struct block *block;
  2037.      enum namespace namespace;
  2038.      enum address_class class;
  2039. {
  2040.     register int    bot, top, inc;
  2041.     register struct symbol *sym;
  2042.  
  2043.     bot = 0;
  2044.     top = BLOCK_NSYMS(block);
  2045.     inc = name[0];
  2046.     while (bot < top) {
  2047.         sym = BLOCK_SYM(block, bot);
  2048.         if (SYMBOL_NAME(sym)[0] == inc
  2049.             && SYMBOL_NAMESPACE(sym) == namespace
  2050.             && SYMBOL_CLASS(sym) == class
  2051.             && !strcmp(SYMBOL_NAME(sym), name))
  2052.             return sym;
  2053.         bot++;
  2054.     }
  2055.     if (block = BLOCK_SUPERBLOCK (block))
  2056.         return mylookup_symbol (name, block, namespace, class);
  2057.     return 0;
  2058. }
  2059.  
  2060.  
  2061. /* Add a new symbol S to a block B.
  2062.    Infrequently, we will need to reallocate the block to make it bigger.
  2063.    We only detect this case when adding to top_stack->cur_block, since
  2064.    that's the only time we know how big the block is.  FIXME.  */
  2065.  
  2066. static void
  2067. add_symbol(s,b)
  2068.     struct symbol *s;
  2069.     struct block *b;
  2070. {
  2071.     int nsyms = BLOCK_NSYMS(b)++;
  2072.     struct block *origb;
  2073.     struct parse_stack *stackp;
  2074.  
  2075.     if (b == top_stack->cur_block &&
  2076.         nsyms >= top_stack->maxsyms) {
  2077.         complain (&block_overflow_complaint, s->name);
  2078.         /* In this case shrink_block is actually grow_block, since
  2079.            BLOCK_NSYMS(b) is larger than its current size.  */
  2080.         origb = b;
  2081.         b = shrink_block (top_stack->cur_block, top_stack->cur_st);
  2082.  
  2083.         /* Now run through the stack replacing pointers to the
  2084.            original block.  shrink_block has already done this
  2085.            for the blockvector and BLOCK_FUNCTION.  */
  2086.         for (stackp = top_stack; stackp; stackp = stackp->next) {
  2087.             if (stackp->cur_block == origb) {
  2088.                 stackp->cur_block = b;
  2089.                 stackp->maxsyms = BLOCK_NSYMS (b);
  2090.             }
  2091.         }
  2092.     }
  2093.     BLOCK_SYM(b,nsyms) = s;
  2094. }
  2095.  
  2096. /* Add a new block B to a symtab S */
  2097.  
  2098. static void
  2099. add_block(b,s)
  2100.     struct block *b;
  2101.     struct symtab *s;
  2102. {
  2103.     struct blockvector *bv = BLOCKVECTOR(s);
  2104.  
  2105.     bv = (struct blockvector *)xrealloc(bv, sizeof(struct blockvector) +
  2106.                         BLOCKVECTOR_NBLOCKS(bv) * sizeof(bv->block));
  2107.     if (bv != BLOCKVECTOR(s))
  2108.         BLOCKVECTOR(s) = bv;
  2109.  
  2110.     BLOCKVECTOR_BLOCK(bv, BLOCKVECTOR_NBLOCKS(bv)++) = b;
  2111. }
  2112.  
  2113. /* Add a new linenumber entry (LINENO,ADR) to a linevector LT.
  2114.    MIPS' linenumber encoding might need more than one byte
  2115.    to describe it, LAST is used to detect these continuation lines */
  2116.  
  2117. static int
  2118. add_line(lt, lineno, adr, last)
  2119.     struct linetable *lt;
  2120.     int lineno;
  2121.     CORE_ADDR adr;
  2122.     int last;
  2123. {
  2124.     if (last == 0)
  2125.         last = -2;    /* make sure we record first line */
  2126.  
  2127.     if (last == lineno)    /* skip continuation lines */
  2128.         return lineno;
  2129.  
  2130.     lt->item[lt->nitems].line = lineno;
  2131.     lt->item[lt->nitems++].pc = adr << 2;
  2132.     return lineno;
  2133. }
  2134.  
  2135.  
  2136.  
  2137. /* Comparison functions, used when sorting things */
  2138.  
  2139. /*  Symtabs must be ordered viz the code segments they cover */
  2140.  
  2141. static int
  2142. compare_symtabs( s1, s2)
  2143.     struct symtab **s1, **s2;
  2144. {
  2145.     /* "most specific" first */
  2146.  
  2147.     register struct block *b1, *b2;
  2148.     b1 = BLOCKVECTOR_BLOCK(BLOCKVECTOR(*s1),GLOBAL_BLOCK);
  2149.     b2 = BLOCKVECTOR_BLOCK(BLOCKVECTOR(*s2),GLOBAL_BLOCK);
  2150.     if (BLOCK_END(b1) == BLOCK_END(b2))
  2151.         return BLOCK_START(b1) - BLOCK_START(b2);
  2152.     return BLOCK_END(b1) - BLOCK_END(b2);
  2153. }
  2154.  
  2155.  
  2156. /*  Partial Symtabs, same */
  2157.  
  2158. static int
  2159. compare_psymtabs( s1, s2)
  2160.     struct partial_symtab **s1, **s2;
  2161. {
  2162.     /* Perf twist: put the ones with no code at the end */
  2163.  
  2164.     register int a = (*s1)->textlow;
  2165.     register int b = (*s2)->textlow;
  2166.     if (a == 0)
  2167.         return b;
  2168.     if (b == 0)
  2169.         return -a;
  2170.     return a - b;
  2171. }
  2172.  
  2173.  
  2174. /* Partial symbols are compared lexicog by their print names */
  2175.  
  2176. static int
  2177. compare_psymbols (s1, s2)
  2178.     register struct partial_symbol *s1, *s2;
  2179. {
  2180.     register char
  2181.                    *st1 = SYMBOL_NAME(s1),
  2182.                    *st2 = SYMBOL_NAME(s2);
  2183.  
  2184.     return (st1[0] - st2[0] ? st1[0] - st2[0] :
  2185.         strcmp(st1 + 1, st2 + 1));
  2186. }
  2187.  
  2188. /* Blocks with a smaller low bound should come first */
  2189.  
  2190. static int compare_blocks(b1,b2)
  2191.     struct block **b1, **b2;
  2192. {
  2193.     register int addr_diff;
  2194.  
  2195.     addr_diff = (BLOCK_START((*b1))) - (BLOCK_START((*b2)));
  2196.     if (addr_diff == 0)
  2197.         return (BLOCK_END((*b1))) - (BLOCK_END((*b2)));
  2198.     return addr_diff;
  2199. }
  2200.  
  2201.  
  2202. /* Sorting and reordering procedures */
  2203.  
  2204. /* Sort the blocks of a symtab S.
  2205.    Reorder the blocks in the blockvector by code-address,
  2206.    as required by some MI search routines */
  2207.  
  2208. static void
  2209. sort_blocks(s)
  2210.     struct symtab *s;
  2211. {
  2212.     struct blockvector *bv = BLOCKVECTOR(s);
  2213.  
  2214.     if (BLOCKVECTOR_NBLOCKS(bv) <= 2) {
  2215.         /* Cosmetic */
  2216.         if (BLOCK_END(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) == 0)
  2217.             BLOCK_START(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) = 0;
  2218.         if (BLOCK_END(BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) == 0)
  2219.             BLOCK_START(BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) = 0;
  2220.         return;
  2221.     }
  2222.     /*
  2223.      * This is very unfortunate: normally all functions are compiled in
  2224.      * the order they are found, but if the file is compiled -O3 things
  2225.      * are very different.  It would be nice to find a reliable test
  2226.      * to detect -O3 images in advance.
  2227.      */
  2228.     if (BLOCKVECTOR_NBLOCKS(bv) > 3)
  2229.         qsort(&BLOCKVECTOR_BLOCK(bv,FIRST_LOCAL_BLOCK),
  2230.               BLOCKVECTOR_NBLOCKS(bv) - FIRST_LOCAL_BLOCK,
  2231.               sizeof(struct block *),
  2232.               compare_blocks);
  2233.  
  2234.     {
  2235.         register CORE_ADDR high = 0;
  2236.         register int    i, j = BLOCKVECTOR_NBLOCKS(bv);
  2237.  
  2238.         for (i = FIRST_LOCAL_BLOCK; i < j; i++)
  2239.             if (high < BLOCK_END(BLOCKVECTOR_BLOCK(bv,i)))
  2240.                 high = BLOCK_END(BLOCKVECTOR_BLOCK(bv,i));
  2241.         BLOCK_END(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) = high;
  2242.     }
  2243.  
  2244.     BLOCK_START(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK)) =
  2245.         BLOCK_START(BLOCKVECTOR_BLOCK(bv,FIRST_LOCAL_BLOCK));
  2246.  
  2247.     BLOCK_START(BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) = 
  2248.         BLOCK_START(BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK));
  2249.     BLOCK_END  (BLOCKVECTOR_BLOCK(bv,STATIC_BLOCK)) =
  2250.         BLOCK_END  (BLOCKVECTOR_BLOCK(bv,GLOBAL_BLOCK));
  2251. }
  2252.  
  2253. /* Sort the symtab list, as required by some search procedures.
  2254.    We want files ordered to make them look right to users, and for
  2255.    searching (see block_for_pc).  */
  2256.  
  2257. static void
  2258. reorder_symtabs()
  2259. {
  2260.     register int i;
  2261.     struct symtab *stab;
  2262.     register struct symtab **all_symtabs;
  2263.     register int symtab_count;
  2264.  
  2265.     if (!symtab_list)
  2266.         return;
  2267.  
  2268.     /* Create an array of pointers to all the symtabs.  */
  2269.     for (symtab_count = 0, stab = symtab_list;
  2270.          stab;
  2271.          symtab_count++, stab = stab->next) {
  2272.         obstack_grow (psymbol_obstack, &stab, sizeof (stab));
  2273.         /* FIXME: Only sort blocks for new symtabs ??? */
  2274.         sort_blocks(stab);
  2275.     }
  2276.  
  2277.     all_symtabs = (struct symtab **)
  2278.         obstack_base (psymbol_obstack);
  2279.     qsort((char *)all_symtabs, symtab_count,
  2280.         sizeof(struct symtab *), compare_symtabs);
  2281.  
  2282.     /* Re-construct the symtab list, but now it is sorted.  */
  2283.     for (i = 0; i < symtab_count-1; i++)
  2284.         all_symtabs[i]->next = all_symtabs[i+1];
  2285.     all_symtabs[i]->next = 0;
  2286.     symtab_list = all_symtabs[0];
  2287.  
  2288.     obstack_free (psymbol_obstack, all_symtabs);
  2289. }
  2290.  
  2291. /* Sort the partial symtab list, as required by some search procedures.
  2292.    PC lookups stop at the first psymtab such that textlow <= PC < texthigh */
  2293.  
  2294. static void
  2295. reorder_psymtabs()
  2296. {
  2297.     register int i;
  2298.     register int all_psymtabs_count;
  2299.     struct partial_symtab *pstab;
  2300.     struct partial_symtab **all_psymtabs;
  2301.  
  2302.     if (!partial_symtab_list)
  2303.         return;
  2304.  
  2305.     /* Create an array of pointers to all the partial_symtabs.  */
  2306.  
  2307.     for (all_psymtabs_count = 0, pstab = partial_symtab_list;
  2308.          pstab;
  2309.          all_psymtabs_count++, pstab = pstab->next)
  2310.       obstack_grow (psymbol_obstack, &pstab, sizeof (pstab));
  2311.  
  2312.     all_psymtabs = (struct partial_symtab **)
  2313.          obstack_base (psymbol_obstack);
  2314.  
  2315.     qsort((char *)all_psymtabs, all_psymtabs_count,
  2316.         sizeof(struct partial_symtab *), compare_psymtabs);
  2317.  
  2318.     /* Re-construct the partial_symtab_list, but now it is sorted.  */
  2319.  
  2320.     for (i = 0; i < all_psymtabs_count-1; i++)
  2321.         all_psymtabs[i]->next = all_psymtabs[i+1];
  2322.     all_psymtabs[i]->next = 0;
  2323.     partial_symtab_list = all_psymtabs[0];
  2324.  
  2325.     obstack_free (psymbol_obstack, all_psymtabs);
  2326. }
  2327.  
  2328. /* Constructor/restructor/destructor procedures */
  2329.  
  2330. /* Allocate a new symtab for NAME.  Needs an estimate of how many symbols
  2331.    MAXSYMS and linenumbers MAXLINES we'll put in it */
  2332.  
  2333. static
  2334. struct symtab *
  2335. new_symtab(name, maxsyms, maxlines)
  2336.     char *name;
  2337. {
  2338.     struct symtab *s = (struct symtab *) xzalloc(sizeof(struct symtab));
  2339.     int i;
  2340.  
  2341.     LINETABLE(s) = new_linetable(maxlines);
  2342.  
  2343.     s->filename = name;
  2344.  
  2345.     /* All symtabs must have at least two blocks */
  2346.     BLOCKVECTOR(s) = new_bvect(2);
  2347.     BLOCKVECTOR_BLOCK(BLOCKVECTOR(s), GLOBAL_BLOCK) = new_block(maxsyms);
  2348.     BLOCKVECTOR_BLOCK(BLOCKVECTOR(s), STATIC_BLOCK) = new_block(maxsyms);
  2349.     BLOCK_SUPERBLOCK( BLOCKVECTOR_BLOCK(BLOCKVECTOR(s),STATIC_BLOCK)) =
  2350.         BLOCKVECTOR_BLOCK(BLOCKVECTOR(s), GLOBAL_BLOCK);
  2351.  
  2352.     s->free_code = free_linetable;
  2353.  
  2354.     /* Link the new symtab into the list of such.  */
  2355.     s->next = symtab_list;
  2356.     symtab_list = s;
  2357.  
  2358.     return s;
  2359. }
  2360.  
  2361. /* Allocate a new partial_symtab NAME */
  2362.  
  2363. static struct partial_symtab *
  2364. new_psymtab(name)
  2365.     char *name;
  2366. {
  2367.     struct partial_symtab *pst;
  2368.  
  2369.     pst = (struct partial_symtab *)
  2370.           obstack_alloc (psymbol_obstack, sizeof (*pst));
  2371.     bzero (pst, sizeof (*pst));
  2372.  
  2373.     if (name == (char*)-1)        /* FIXME -- why not null here? */
  2374.         pst->filename = "<no name>";
  2375.     else
  2376.         pst->filename = name;
  2377.  
  2378.     pst->next = partial_symtab_list;
  2379.     partial_symtab_list = pst;
  2380.  
  2381.     /* Keep a backpointer to the file`s symbols */
  2382.     /* FIXME, we should use private data that is a proper pointer. */
  2383.     pst->ldsymlen = (int)cur_hdr;
  2384.  
  2385.     /* The way to turn this into a symtab is to call... */
  2386.     pst->read_symtab = mipscoff_psymtab_to_symtab;
  2387.  
  2388.     return pst;
  2389. }
  2390.  
  2391.  
  2392. /* Allocate a linetable array of the given SIZE */
  2393.  
  2394. static
  2395. struct linetable *new_linetable(size)
  2396. {
  2397.     struct linetable *l;
  2398.  
  2399.     size = size * sizeof(l->item) + sizeof(struct linetable);
  2400.     l = (struct linetable *)xmalloc(size);
  2401.     l->nitems = 0;
  2402.     return l;
  2403. }
  2404.  
  2405. /* Oops, too big. Shrink it.  This was important with the 2.4 linetables,
  2406.    I am not so sure about the 3.4 ones */
  2407.  
  2408. static void
  2409. shrink_linetable(s)
  2410.     struct symtab *s;
  2411. {
  2412.     struct linetable *l = new_linetable(LINETABLE(s)->nitems);
  2413.  
  2414.     bcopy(LINETABLE(s), l,
  2415.           LINETABLE(s)->nitems * sizeof(l->item) + sizeof(struct linetable));
  2416.     free (LINETABLE(s));
  2417.     LINETABLE(s) = l;
  2418. }
  2419.  
  2420. /* Allocate and zero a new blockvector of NBLOCKS blocks. */
  2421.  
  2422. static
  2423. struct blockvector *
  2424. new_bvect(nblocks)
  2425. {
  2426.     struct blockvector *bv;
  2427.     int size;
  2428.  
  2429.     size = sizeof(struct blockvector) + nblocks * sizeof(struct block*);
  2430.     bv = (struct blockvector *) xzalloc(size);
  2431.  
  2432.     BLOCKVECTOR_NBLOCKS(bv) = nblocks;
  2433.  
  2434.     return bv;
  2435. }
  2436.  
  2437. /* Allocate and zero a new block of MAXSYMS symbols */
  2438.  
  2439. static
  2440. struct block *
  2441. new_block(maxsyms)
  2442. {
  2443.     int size = sizeof(struct block) + (maxsyms-1) * sizeof(struct symbol *);
  2444.     struct block *b = (struct block *)xzalloc(size);
  2445.  
  2446.     return b;
  2447. }
  2448.  
  2449. /* Ooops, too big. Shrink block B in symtab S to its minimal size.
  2450.    Shrink_block can also be used by add_symbol to grow a block.  */
  2451.  
  2452. static struct block *
  2453. shrink_block(b, s)
  2454.     struct block *b;
  2455.     struct symtab *s;
  2456. {
  2457.     struct block *new;
  2458.     struct blockvector *bv = BLOCKVECTOR(s);
  2459.     int i;
  2460.  
  2461.     /* Just reallocate it and fix references to the old one */
  2462.  
  2463.     new = (struct block *) xrealloc ((char *)b, sizeof(struct block) +
  2464.           (BLOCK_NSYMS(b)-1) * sizeof(struct symbol *));
  2465.  
  2466.     /* Should chase pointers to old one.  Fortunately, that`s just
  2467.        the block`s function and inferior blocks */
  2468.     if (BLOCK_FUNCTION(new) && SYMBOL_BLOCK_VALUE(BLOCK_FUNCTION(new)) == b)
  2469.         SYMBOL_BLOCK_VALUE(BLOCK_FUNCTION(new)) = new;
  2470.     for (i = 0; i < BLOCKVECTOR_NBLOCKS(bv); i++)
  2471.         if (BLOCKVECTOR_BLOCK(bv,i) == b)
  2472.             BLOCKVECTOR_BLOCK(bv,i) = new;
  2473.         else if (BLOCK_SUPERBLOCK(BLOCKVECTOR_BLOCK(bv,i)) == b)
  2474.             BLOCK_SUPERBLOCK(BLOCKVECTOR_BLOCK(bv,i)) = new;
  2475.     return new;
  2476. }
  2477.  
  2478. /* Create a new symbol with printname NAME */
  2479.  
  2480. static
  2481. struct symbol *
  2482. new_symbol(name)
  2483.     char *name;
  2484. {
  2485.     struct symbol *s = (struct symbol *) 
  2486.         obstack_alloc (symbol_obstack, sizeof (struct symbol));
  2487.  
  2488.     bzero (s, sizeof (*s));
  2489.     SYMBOL_NAME(s) = name;
  2490.     return s;
  2491. }
  2492.  
  2493. /* Create a new type with printname NAME */
  2494.  
  2495. static
  2496. struct type *
  2497. new_type(name)
  2498.     char *name;
  2499. {
  2500.     struct type *t = (struct type *)
  2501.         obstack_alloc (symbol_obstack, sizeof (struct type));
  2502.  
  2503.     bzero (t, sizeof (*t));
  2504.     TYPE_NAME(t) = name;
  2505.     return t;
  2506. }
  2507.  
  2508. /* Create and initialize a new type with printname NAME.
  2509.    CODE and LENGTH are the initial info we put in,
  2510.    UNS says whether the type is unsigned or not.  */
  2511.  
  2512. static
  2513. struct type *
  2514. make_type(code, length, uns, name)
  2515.      enum type_code code;
  2516.      int length, uns;
  2517.      char *name;
  2518. {
  2519.     register struct type *type;
  2520.  
  2521.     type = (struct type *) xzalloc(sizeof(struct type));
  2522.     TYPE_CODE(type) = code;
  2523.     TYPE_LENGTH(type) = length;
  2524.     TYPE_FLAGS(type) = uns ? TYPE_FLAG_UNSIGNED : 0;
  2525.     TYPE_NAME(type) = name;
  2526.  
  2527.     return type;
  2528. }
  2529.  
  2530. /* Allocate a new field named NAME to the type TYPE */
  2531.  
  2532. static
  2533. struct field *
  2534. new_field(type,name)
  2535.     struct type *type;
  2536.     char *name;
  2537. {
  2538.     struct field *f;
  2539.  
  2540.     /* Fields are kept in an array */
  2541.     if (TYPE_NFIELDS(type))
  2542.         TYPE_FIELDS(type) = (struct field*)xrealloc(TYPE_FIELDS(type),
  2543.             (TYPE_NFIELDS(type)+1) * sizeof(struct field));
  2544.     else
  2545.         TYPE_FIELDS(type) = (struct field*)xzalloc(sizeof(struct field));
  2546.     f = &(TYPE_FIELD(type,TYPE_NFIELDS(type)));
  2547.     TYPE_NFIELDS(type)++;
  2548.     bzero(f, sizeof(struct field));
  2549.     f->name = name;        /* Whether or not NAME is zero, this works. */
  2550.     return f;
  2551. }
  2552.  
  2553. /* Make an enum constant for a member F of an enumerated type T */
  2554.  
  2555. static
  2556. make_enum_constant(f,t)
  2557.     struct field *f;
  2558.     struct type *t;
  2559. {
  2560.     struct symbol *s;
  2561.     /*
  2562.      * This is awful, but that`s the way it is supposed to be
  2563.      * (BTW, no need to free the real 'type', it's a builtin)
  2564.      */
  2565.     f->type = (struct type *) f->bitpos;
  2566.  
  2567.     s = new_symbol(f->name);
  2568.     SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
  2569.     SYMBOL_CLASS(s) = LOC_CONST;
  2570.     SYMBOL_TYPE(s) = t;
  2571.     SYMBOL_VALUE(s) = f->bitpos;
  2572.     add_symbol(s, top_stack->cur_block);
  2573. }
  2574.  
  2575.  
  2576.  
  2577. /* Things used for calling functions in the inferior.
  2578.    These functions are exported to our companion
  2579.    mips-dep.c file and are here because they play
  2580.    with the symbol-table explicitly. */
  2581.  
  2582. #if 0
  2583. /* Need to make a new symbol on the fly for the dummy
  2584.    frame we put on the stack.  Which goes in the.. */
  2585.  
  2586. static struct symtab *dummy_symtab;
  2587.  
  2588. /* Make up a dummy symbol for the code we put at END_PC,
  2589.    of size SIZE, invoking a function with NARGS arguments
  2590.    and using a frame of FRAMESIZE bytes */
  2591.  
  2592. mips_create_dummy_symbol(end_pc, size, nargs, framesize)
  2593. {
  2594.     struct block   *bl;
  2595.     struct symbol  *g;
  2596.     struct mips_extra_func_info *gdbinfo;
  2597.  
  2598.     /* Allocate symtab if not done already */
  2599.     if (dummy_symtab == 0)
  2600.         dummy_symtab = new_symtab(".dummy_symtab.", 100, 0);
  2601.  
  2602.     /* Make a new block. Only needs one symbol */
  2603.     bl = new_block(1);
  2604.     BLOCK_START(bl) = end_pc - size;
  2605.     BLOCK_END(bl) = end_pc;
  2606.  
  2607.     BLOCK_SUPERBLOCK(bl) =
  2608.         BLOCKVECTOR_BLOCK(BLOCKVECTOR(dummy_symtab),GLOBAL_BLOCK);
  2609.     add_block(bl, dummy_symtab);
  2610.     sort_blocks(dummy_symtab);
  2611.  
  2612.     BLOCK_FUNCTION(bl) = new_symbol("??");
  2613.     SYMBOL_BLOCK_VALUE(BLOCK_FUNCTION(bl)) = bl;
  2614.     g = new_symbol(".gdbinfo.");
  2615.     BLOCK_SYM(bl,BLOCK_NSYMS(bl)++) = g;
  2616.  
  2617.     SYMBOL_NAMESPACE(g) = LABEL_NAMESPACE;
  2618.     SYMBOL_CLASS(g) = LOC_CONST;
  2619.     SYMBOL_TYPE(g) = builtin_type_void;
  2620.     gdbinfo = (struct mips_extra_func_info *)
  2621.         xzalloc(sizeof(struct mips_extra_func_info));
  2622.  
  2623.     SYMBOL_VALUE(g) = (long) gdbinfo;
  2624.  
  2625.     gdbinfo->numargs = nargs;
  2626.     gdbinfo->framesize = framesize;
  2627.     gdbinfo->framereg = 29;
  2628.     gdbinfo->pcreg = 31;
  2629.     gdbinfo->regmask = -2;
  2630.     gdbinfo->regoffset = -4;
  2631.     gdbinfo->fregmask = 0;    /* XXX */
  2632.     gdbinfo->fregoffset = 0;    /* XXX */
  2633. }
  2634.  
  2635. /* We just returned from the dummy code at END_PC, drop its symbol */
  2636.  
  2637. mips_destroy_dummy_symbol(end_pc)
  2638. {
  2639.     struct block *bl;
  2640.     struct blockvector *bv = BLOCKVECTOR(dummy_symtab);
  2641.     int i;
  2642.  
  2643.     bl = block_for_pc(end_pc);
  2644.     free(BLOCK_FUNCTION(bl));
  2645.     free(SYMBOL_VALUE(BLOCK_SYM(bl,0)));
  2646.     free(BLOCK_SYM(bl,0));
  2647.  
  2648.     for (i = FIRST_LOCAL_BLOCK; i < BLOCKVECTOR_NBLOCKS(bv); i++)
  2649.         if (BLOCKVECTOR_BLOCK(bv,i) == bl)
  2650.             break;
  2651.     for (; i < BLOCKVECTOR_NBLOCKS(bv) - 1; i++)
  2652.         BLOCKVECTOR_BLOCK(bv,i) = BLOCKVECTOR_BLOCK(bv,i+1);
  2653.     BLOCKVECTOR_NBLOCKS(bv)--;
  2654.     sort_blocks(dummy_symtab);
  2655.     free(bl);
  2656. }
  2657. #endif
  2658.  
  2659. /* Sigtramp: make sure we have all the necessary information
  2660.    about the signal trampoline code. Since the official code
  2661.    from MIPS does not do so, we make up that information ourselves.
  2662.    If they fix the library (unlikely) this code will neutralize itself. */
  2663.  
  2664. static
  2665. fixup_sigtramp()
  2666. {
  2667.     struct symbol  *s;
  2668.     struct symtab  *st;
  2669.     struct block   *b, *b0;
  2670.  
  2671.     sigtramp_address = -1;
  2672.  
  2673.     /* We know it is sold as sigvec */
  2674.     s = lookup_symbol("sigvec", 0, VAR_NAMESPACE, 0, NULL);
  2675.  
  2676.     /* Most programs do not play with signals */
  2677.     if (s == 0)
  2678.         return;
  2679.  
  2680.     b0 = SYMBOL_BLOCK_VALUE(s);
  2681.  
  2682.     /* A label of sigvec, to be more precise */
  2683.     s = lookup_symbol("sigtramp", b0, VAR_NAMESPACE, 0, NULL);
  2684.  
  2685.     /* But maybe this program uses its own version of sigvec */
  2686.     if (s == 0)
  2687.         return;
  2688.  
  2689.     sigtramp_address = SYMBOL_VALUE(s);
  2690.     sigtramp_end = sigtramp_address + 0x88;    /* black magic */
  2691.  
  2692.     /* Did we or MIPSco fix the library ? */
  2693.     if (SYMBOL_CLASS(s) == LOC_BLOCK)
  2694.         return;
  2695.  
  2696.     /* But what symtab does it live in ? */
  2697.     st = find_pc_symtab(SYMBOL_VALUE(s));
  2698.  
  2699.     /*
  2700.      * Ok, there goes the fix: turn it into a procedure, with all the
  2701.      * needed info.  Note we make it a nested procedure of sigvec,
  2702.      * which is the way the (assembly) code is actually written.
  2703.      */
  2704.     SYMBOL_NAMESPACE(s) = VAR_NAMESPACE;
  2705.     SYMBOL_CLASS(s) = LOC_BLOCK;
  2706.     SYMBOL_TYPE(s) = make_type(TYPE_CODE_FUNC, 4, 0, 0);
  2707.     TYPE_TARGET_TYPE(SYMBOL_TYPE(s)) = builtin_type_void;
  2708.  
  2709.     /* Need a block to allocate .gdbinfo. in */
  2710.     b = new_block(1);
  2711.     SYMBOL_BLOCK_VALUE(s) = b;
  2712.     BLOCK_START(b) = sigtramp_address;
  2713.     BLOCK_END(b) = sigtramp_end;
  2714.     BLOCK_FUNCTION(b) = s;
  2715.     BLOCK_SUPERBLOCK(b) = BLOCK_SUPERBLOCK(b0);
  2716.     add_block(b, st);
  2717.     sort_blocks(st);
  2718.  
  2719.     /* Make a .gdbinfo. for it */
  2720.     {
  2721.         struct mips_extra_func_info *e =
  2722.             (struct mips_extra_func_info *)
  2723.             xzalloc(sizeof(struct mips_extra_func_info));
  2724.  
  2725.         e->numargs = 0;    /* the kernel thinks otherwise */
  2726.         /* align_longword(sigcontext + SIGFRAME) */
  2727.         e->framesize = 0x150;
  2728.         e->framereg = SP_REGNUM;
  2729.         e->pcreg = 31;
  2730.         e->regmask = -2;
  2731.         e->regoffset = -(41 * sizeof(int));
  2732.         e->fregmask = -1;
  2733.         e->fregoffset = -(37 * sizeof(int));
  2734.         e->isym = (long)s;
  2735.  
  2736.         s = new_symbol(".gdbinfo.");
  2737.         SYMBOL_VALUE(s) = (int) e;
  2738.         SYMBOL_NAMESPACE(s) = LABEL_NAMESPACE;
  2739.         SYMBOL_CLASS(s) = LOC_CONST;
  2740.         SYMBOL_TYPE(s) = builtin_type_void;
  2741.     }
  2742.  
  2743.     BLOCK_SYM(b,BLOCK_NSYMS(b)++) = s;
  2744. }
  2745.  
  2746. /* Initialization */
  2747.  
  2748. static struct sym_fns ecoff_sym_fns = {"ecoff", 5,
  2749.         mipscoff_new_init, mipscoff_symfile_init,
  2750.         mipscoff_symfile_read};
  2751.  
  2752. _initialize_mipsread ()
  2753. {
  2754.     add_symtab_fns (&ecoff_sym_fns);
  2755.  
  2756.     /* Missing basic types */
  2757.     builtin_type_string = make_type(TYPE_CODE_PASCAL_ARRAY,
  2758.                     1, 0, "string");
  2759.     builtin_type_complex = make_type(TYPE_CODE_FLT,
  2760.                      2 * sizeof(float), 0, "complex");
  2761.     builtin_type_double_complex = make_type(TYPE_CODE_FLT,
  2762.                     2 * sizeof(double), 0, "double_complex");
  2763.     builtin_type_fixed_dec = make_type(TYPE_CODE_INT, sizeof(int),
  2764.                        0, "fixed_decimal");
  2765.     builtin_type_float_dec = make_type(TYPE_CODE_FLT, sizeof(double),
  2766.                        0, "floating_decimal");
  2767.  
  2768.     /* Templates types */
  2769.     builtin_type_ptr = lookup_pointer_type (builtin_type_void);
  2770.     builtin_type_struct = make_type(TYPE_CODE_STRUCT, 0, 0, 0);
  2771.     builtin_type_union = make_type(TYPE_CODE_UNION, 0, 0, 0);
  2772.     builtin_type_enum = make_type(TYPE_CODE_ENUM, 0, 0, 0);
  2773.     builtin_type_range = make_type(TYPE_CODE_RANGE, 0, 0, 0);
  2774.     builtin_type_set = make_type(TYPE_CODE_SET, 0, 0, 0);
  2775. }
  2776.